javascript - Why does Function have both implicit and explicit prototype references, and can I set the implicit reference? -


after reading documentation: http://es5.github.io/#x4.2.1

i confused 2 prototype references on cf, , statement:

the property named cfp1 in cfp shared cf1, cf2, cf3, cf4, , cf5 (but not cf)

much of literature on javascript points out functions first class objects, , such i'd expect able set implicit prototype reference object achieve prototypal inheritance (disclaimer: don't know i'd use inheritance for, occurred me see if it's possible). can set implicit prototype on function, or point function.prototype (i'm assuming that's default). , why function have both explicit , implicit prototypes? other types in javascript have both explicit , implicit prototype references or function unique in regard?

consider diagram specification, , code below it, tries reproduce what's going on:

enter image description here

function cf() {};            // constructor cf.p1 = 'foo';               // p1 own property of constructor; p2 same var cfp = { crp1: 'bar' };   // now, object, crp1 property cf.prototype = cfp           // set cfp 'explicit prototype property' of cf;                              // constructors have such property var cf1 = new cf();          // instance;  var cf2 = new cf();          // instance; cf3..cf5 constructed same way object.getprototypeof(cf1);  // cfp; 'implicit prototype link' cf1 cfp;                              // put way, cfp became [[prototype]] of cf1 

you said confused sentence: the property named cfp1 in cfp shared cf1, cf2, cf3, cf4, , cf5 (but not cf). consider this:

cf1.crp1;   // 'bar' - found on cfp through cf1 cf2.crp1;   // 'bar' - found on cfp through cf2 cf.crp1;    // undefined 

so sentence means can access contents of crp1 cf1..cf5, not constructor cf (remember, functions/constructors objects too, can have properties). , that's because cfp (the "owner" of crp1) not [[prototype]] of cf, it's value pointed cf.prototype property. prototype property exist in function objects, , used solely define [[prototype]] of instances created invocations of function invoked constructor (like in new cf()). fact both [[prototype]] , prototype read "prototype" source of great confusion – , maybe part of confusing you; hopefully, it's less confusing now. in mind, i'll try shortly answer other questions.

much of literature on javascript points out functions first class objects, , such i'd expect able set implicit prototype reference object achieve prototypal inheritance [...].

in es5 there no way directly set implicit prototype reference (or [[prototype]]) of existing object, except non-standard __proto__ property. can create new objects given [[prototype]]. can var obj = new constructorfunction(), [[prototype]] of obj constructorfunction.prototype, or var obj = object.create(someotherobj), [[prototype]] of obj someotherobj. later versions of language introduced object.setprototypeof that, use discouraged performance reasons.

can set implicit prototype on function, or point function.prototype (i'm assuming that's default).

yes, __proto__ or object.setprototypeof. shouldn't.

and why function have both explicit , implicit prototypes? other types in javascript have both explicit , implicit prototype references or function unique in regard?

function ("the function constructor") function, , other function has prototype property; it's object, , (almost) other object has [[prototype]] object. there standard constructors other types too, object, string, array, boolean, number. they're functions, , have both prototype , [[prototype]].


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -