www.webdeveloper.com
Results 1 to 6 of 6

Thread: Closure Question

  1. #1
    Join Date
    May 2012
    Posts
    32

    Closure Question

    Hello I am learning JavaScript.

    In regards to the below shim why did the author put var F outside of the anonymous function(o) and create a closure. Wouldn't var F inside of function(o) have worked also?

    Thank you.

    Code:
    if (typeof Object.create != 'function') {
      (function () {
        // why is this outside of Object.Create.
    	var F = function () {};
        Object.create = function (o) {
          if (arguments.length > 1) { throw Error('Second argument not supported');}
          if (o === null) { throw Error('Cannot set a null [[Prototype]]');}
          if (typeof o != 'object') { throw TypeError('Argument must be an object');}
          F.prototype = o;
          return new F;
        };
      })();
    }

  2. #2
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Probably to avoid it being recreated with every invocation. Better performance.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  3. #3
    Join Date
    Aug 2010
    Location
    San Diego
    Posts
    242
    If F were declared in the Object.create function, a new version of F would be created each time Object.create is called. With F declared outside Object.create, the same F will be used each time Object.create is called.

    The semantics (of F inside vs. outside) are technically different, but in practicality I don't see any difference, except as Jeff Mott mentioned the performance issue. Where'd you find this code?
    Last edited by handcraftedweb; 06-01-2012 at 12:24 PM.

  4. #4
    Join Date
    May 2012
    Posts
    32
    Probably to avoid it being recreated with every invocation. Better performance.
    Ahh I see; by putting it outside it becomes a static variable of function (o).

    So all variables outside of the inner function that forms the closure are static variables of that inner function. Correct?


    Where'd you find this code?
    http://stackoverflow.com/questions/3...roperty-in-es5

    Thank you for the response.
    Last edited by cations; 06-01-2012 at 12:57 PM.

  5. #5
    Join Date
    May 2012
    Posts
    32
    Does the below code produce the identical result as the originally posted code?

    Thank you.

    New Code:

    Code:
    if (typeof Object.create != 'function') {
      (function () {
    	
    	Object.create = function (o) {
    		
    		if (arguments.length > 1) { throw Error('Second argument not supported');}
    		if (o === null) { throw Error('Cannot set a null [[Prototype]]');}
    		if (typeof o != 'object') { throw TypeError('Argument must be an object');}
    		
                    //create a new object
    	        var newObject = {};
    
    		//set the newObject's prototype.
    		newObject.prototype = o;	
    		
    		//return newObject
    		return newObject;
    	};
      })();
    }
    Original Code

    Code:
    if (typeof Object.create != 'function') {
      (function () {
        // why is this outside of Object.Create.
    	var F = function () {};
        Object.create = function (o) {
          if (arguments.length > 1) { throw Error('Second argument not supported');}
          if (o === null) { throw Error('Cannot set a null [[Prototype]]');}
          if (typeof o != 'object') { throw TypeError('Argument must be an object');}
          F.prototype = o;
          return new F;
        };
      })();
    }
    Last edited by cations; 06-01-2012 at 01:08 PM.

  6. #6
    Join Date
    May 2012
    Posts
    32
    Please ignore the last question. I thought the prototype object was stored in the property prototype but I just learned it's stored in __proto__ which is now deprecated, hence the reason for the original code.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles