javascript - difference between Object.create(Object.prototype) , Object.create(Object) and Object.create(null) -
which parameter should pass first parent object others inherit , 1 more efficient
object.create(object.prototype) object.create(object) object.create(null)
object.create(null)
returns empty object
object.create(object)
returns function why????( checked log , says function...i used console.dir() )
object.create(object)
returns non empty object
how whole thing work ... m more used classname .prototype thing :(
can't understand going on here
preface: javascript uses prototypical inheritance, means object can have (usually have) prototype behind it, object. if try value of property object doesn't have, javascript engine looks object's prototype (and prototype, , on) find it.
object.create
creates objects. first argument give object.create
object use prototype of object creates. so:
// create object property 'foo' var = { foo: 42 }; // create blank object using `a` prototype var b = object.create(a); // give `b` property of own b.bar = "hi";
that gives in memory:
+---------------+ +-------------------+ | [[prototype]] |----->| (the standard | a----------------------+-->| foo: 42 | | object prototype) | | +---------------+ +-------------------+ | +---------------+ | b-->| [[prototype]] |--+ | bar: "hi" | +---------------+
proof b
uses a
:
console.log(b.foo); // 42 a.foo = 67; console.log(b.foo); // 67
addressing of variations:
var o = object.create(object.prototype);
that's pointless, use var o = {};
, same thing (creates new blank object prototype object.prototype
).
var o = object.create(object);
creates new blank object o
prototype object
function. doesn't create function, non-function object has function prototype. quite odd , isn't want.
var o = object.create(null);
creates new blank object o
prototype null
. since prototype null
, doesn't have usual object.prototype
stuff, tostring
, valueof
, hasownproperty
. that's bit unusual, although there use cases it, such when you're using object dictionary/map , don't want false positives property names. (in es2015 [aka es6] option use map
instead.)
as thg435 points out in comment below, 1 of confusing things javascript prototype of object different thing prototype
property see on functions. better if prototype
property had had different name (although can't imagine name without being massively clunky).
an object (let's call o
) has prototype object inherits properties from. object on prototype
property of functions not prototype of object @ all. instead, it's object will be assigned prototype of object created via new
using function.
examples here.
function foo() { }
that function, foo
, has property foo.prototype
refers object. object not, yet, used prototype of anything. it's object assigned property called prototype
on foo
object instance.
var f = new foo();
now object used prototype, it's prototype of f
object created new foo
call.
ignoring couple of details, line of code:
var f = new foo();
...basically this:
// create blank object, giving `foo.prototype` prototype var f = object.create(foo.prototype); // call` foo` using new object `this` foo.call(f);
as say, leaves out couple of details, helps make clear prototype
property of functions for...
Comments
Post a Comment