1

I was looking at the code of the new operator in the Douglas Crockford series.

function Shane(name){
    this.name = name;    
}

var sha = new Shane("name");
console.log(sha);

The above code just creates a new Shane object and sets its constructor to Shane and Prototype to Object.prototype:

function new(func, args){
     var that = Object.create(func.prototype); 
         result = func.apply(that, args);
     return (typeof result==='object' && result) || that;
}

Can anyone explain me on what this code does and give me an example for it?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Shane
  • 5,517
  • 15
  • 49
  • 79
  • 1
    "*Can anyone explain me on what this code does*" ... It throws a syntax error: `Unexpected token new` – p.s.w.g Jan 16 '15 at 03:55
  • @p.s.w.g: I am asking on how the function new works? I am not using the function new to construct an object. – Shane Jan 16 '15 at 03:57
  • Start with MDN: [new operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new) – p.s.w.g Jan 16 '15 at 03:59
  • @p.s.w.g: I know how new works, i am not understanding the function new() on what it does? – Shane Jan 16 '15 at 04:00
  • 1
    No, neither `new` nor `new()` do anything about setting constructors. – Bergi Jan 16 '15 at 04:04
  • @Shane: Well, if you know how `new` works, then you should know that `new()` just does the same things (except for constructors returning functions) – Bergi Jan 16 '15 at 04:05

2 Answers2

2

The above code just creates a new Shane object and sets its constructor to Shane and Prototype to Object.prototype.

Not quite. The prototype of the object sha will be Shane.prototype. The prototype of Shane.prototype will be Object.prototype.

The function you present (better named new2, gnu, knew or some other variant of new, since it's a syntax error as it stands) performs the same three essential actions that the new operator in the language does.

new Shane(arg1, ..., argN)

does these three things:

  • Creates a new empty object and sets its [[Prototype]] property to Shane.prototype.
  • Applies the function Shane and the parameters arg1, ... argN in the context of the new object, potentially receiving a return value.
  • If a return value is received, return that. If not return this newly created object.

The new2 function (let's call it) does these same things. So you could use it like this:

var sha2 = new2(Shane, ["name"]);

and get back something very similar to sha above.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • return (typeof result==='object' && result) || that; when will that be returned in the function? i mean in which case. – Shane Jan 16 '15 at 17:37
  • If the constructor function has a `return` statement and returns something other than the `this` object, you'll likely follow that branch. – Scott Sauyet Jan 16 '15 at 23:55
  • If its possible can you show some code regarding that for understanding. – Shane Jan 17 '15 at 13:19
  • 1
    `var Foo = function(x) {this.a = x; return {b: x};}; var foo = new Foo(5);` Now `foo.a` is `undefined` because the object being created during construction was discarded in favor of the object literal that was returned instead. But `foo.b` has value `5`. The object `{b: 5}` is assigned to `foo` and not the `this` object that was being enhanced by the constructor. – Scott Sauyet Jan 18 '15 at 02:24
1

here new Shane("name")is equivalent to: new1 (Shane,['name']);

function Shane(name){
        this.name = name;    
    }

var sha= new1 (Shane,['name']);
        console.log(sha);
        function new1(func, args){
            console.log(func,args);
             var that = Object.create(func.prototype);
 //creates an object based on func if func=null then created object  doesn't inherit from anything,
                 result = func.apply(that, args);//applys arguments which should be an array
             return (typeof result==='object' && result) || that;
// if result is received and is object then return result else return that which is newly created object
        }

here `Object.create` builds an object that inherits directly from the one passed as its first argument.

read about apply here:here

see this post for more about new and Object.create:here

Community
  • 1
  • 1
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44