SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    The with keyword, should it be used?

    Hi all,

    Recently while reading through the ECMAScript standard, I came across the with keyword, which essentially seems to change the scope within it to whatever you pass to it. For example,

    Code:
    function FooBar(){}
    with(FooBar.prototype){
       doSomething = function(){
           return 2;
       }
    }
    
    var f = new FooBar();
    alert(f.doSomething()); // alerts "2"
    My question is... is there any drawback to using it if you know not to use anything that could be in the global scope inside of it? Because that is all I was able to find so far... that there is no guarantee that it will use the locally scope variable if one with the same name exists in the global scope.

    I did find a blog post saying it was being deprecated, but the blog post did not list any references so I have no idea if this is true or just rumors.

    Thanks,
    James

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't use it. It's slow and can cause strange behavior.

    I suggest you read Optimizing JavaScript for Execution Speed.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •