Confusion in javascript exercise

I just got Javascript: “Good Details” by Douglas Crockford, and I had difficulty understanding one of his examples regarding prototypes. The code in the book reads as follows:

if (typeof Object.create !== "function") { Object.create = function(o) { var F = function () {} F.prototype = o; return new F; }; } 

I assume this code is used to target a function prototype. But why use such an integrated approach? Why not just use variable .prototype? Crockford is a leading Javascript expert, so I'm sure there is a good reason to use this model. Can someone help me figure this out better? Any help would be appreciated.

+6
source share
4 answers

In ECMAScript 3, the new operator was the only standard way to set the internal [[Prototype]] property of an object, in which case Crockford uses only the F temporary constructor function for this purpose.

The o argument of this method is set as the prototype property of the temporary constructor, and by calling new F(); , it creates a new empty object that inherits from F.prototype (see this question for more details on how new works).

For instance:

 var a = { a: 1 }; var b = Object.create(a); // b inherits from a ba; // 1 

In the above example, we can say that the b internal [[Prototype]] property points to a .

 Object.getPrototypeOf(b) === a; // true 

In other words, b inherits from a .

In the same example, we could use an empty constructor, for example:

  function F(){} F.prototype = a; var b = new F(); // b again inherits from a (F.prototype) 

Remember also that the prototype function property is different from the [[Prototype]] property that all objects have, the prototype function property is used when they are called with a new operator to create a new object that inherits from this property.

In addition, keep in mind that now the ECMAScript 5 standard is implemented, and this gasket does not meet 100% of the specification, in fact there are some features of the standard Object.create method that cannot be emulated on ES3 .

See also:

+5
source
 var bar = Object.create(foo) 

against.

 var bar = new Object() 

the first bar has foo as its prototype; the second has Object as its prototype.

+3
source

This code is for older JavaScript implementations that do not support Object.create , which is specified in the ECMAScript 5 standard released in November 2009.

Many say that the preferred way to create an object is to specify a prototype for it at creation time. This can be called differential inheritance or prototypal inheritance . In fact, this is what Object.create does:

 var protoCircle = {x: 0, y: 0, radius: 1, color:"black"}; var myCircle = Object.create(protoCircle); myCircle.x = 3; myCircle.color = "green"; 

This will create a green circle of radius 1 centered at (3.0).

The reason the code is so complex that Object.create was added to JavaScript before Object.create only way to set up a prototype of an object is to create it using the new operator. Objects created with new have got the value of the prototype constructor property as their prototype . This is definitely confusing, but the prototype property is NOT a prototype of an object. For an object of function f , f.prototype is an object that will be assigned as a prototype of all objects built with new f() . The actual prototype of the object is called [[prototype]] or __proto__ , but you cannot access them in standard ECMAScript. Confuses, and ??

As a note. The ES5 specification has a more extended Object.prototype specification than that defined by Crockford. A second object is required to configure the properties of the defined object.

+1
source

This create method will instantiate a new object, given the passed object as a prototype.

0
source

Source: https://habr.com/ru/post/894229/


All Articles