prototype - JavaScript Protoype confusion -
i new javascript , experimenting these days. came know javascript prototype based. tried executing below code:
var person = function(name) { this.name = name; } alert(person.prototype); var ram = new person("ram"); alert(ram.prototype);
as expected, first alert prints [object, object], expect second alert prints same, doesn't, why?
can please explain clearly?
because prototype
property on functions isn't function's prototype, it's property used set prototype of objects created via new
operator function.
you can access object's prototype using es5's object.getprototypeof
:
var person = function(name) { this.name = name; }; alert(person.prototype); var ram = new person("ram"); alert(object.getprototypeof(ram));
on firefox , several other browsers, can use upcoming es6 feature (for javascript in browsers): __proto__
accessor property:
var person = function(name) { this.name = name; }; alert(person.prototype); var ram = new person("ram"); alert(ram.__proto__); // es6 feature, common
note __proto__
property inherited object.prototype
, if create object doesn't inherit object.prototype
(e.g., var o = object.create(null);
or using o
prototype), can use object.getprototypeof
, not __proto__
.
Comments
Post a Comment