最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

oop - In Javascript, why is there no "prototype" property for an instance or object literal? - Stack Overflow

programmeradmin2浏览0评论

In Javascript, any "function object" has a prototype

> F = function() {}
F()
> F.prototype
F {}

But "object" or "instance" doesn't have a prototype

> o = {}
Object {}
> o.prototype
undefined
> f = new F()
F {}
> f.prototype
undefined

However, the built-in object "Function" and "Object" have a prototype:

> Function.prototype
Empty()
> Object.prototype
Object {}

This looks quite confusing for me.

  1. Function and "function object" have a prototype property

  2. Object has a prototype property, but "object literal" and "instance object" doesn't have a prototype property

What does the prototype property actually mean? In the example above, shouldn't the prototype property of f be F?

Does anyone have ideas about how to explain this? Thanks!

In Javascript, any "function object" has a prototype

> F = function() {}
F()
> F.prototype
F {}

But "object" or "instance" doesn't have a prototype

> o = {}
Object {}
> o.prototype
undefined
> f = new F()
F {}
> f.prototype
undefined

However, the built-in object "Function" and "Object" have a prototype:

> Function.prototype
Empty()
> Object.prototype
Object {}

This looks quite confusing for me.

  1. Function and "function object" have a prototype property

  2. Object has a prototype property, but "object literal" and "instance object" doesn't have a prototype property

What does the prototype property actually mean? In the example above, shouldn't the prototype property of f be F?

Does anyone have ideas about how to explain this? Thanks!

Share Improve this question edited Jun 30, 2015 at 7:22 Hanfei Sun asked Jun 30, 2015 at 7:20 Hanfei SunHanfei Sun 47.2k41 gold badges135 silver badges252 bronze badges 1
  • 4 An instance is an object, and objects are not functions. Object and Function are functions (constructors). Note "object" (as in {}) vs "Object" as in anything that is a JavaScript object. – elclanrs Commented Jun 30, 2015 at 7:22
Add a ment  | 

1 Answer 1

Reset to default 9

Don't confuse the prototype attribute of a function with the internal prototype of an object (which places the object in the prototype chain).

Function and Object are constructor functions, as such, they have a prototype attribute, which will be assigned as the internal prototype of objects created with these constructor functions.

I remend Eloquent Javascript's chapter "The secret life of objects", particularly for this quote:

It is important to note the distinction between the way a prototype is associated with a constructor (through its prototype property) and the way objects have a prototype (which can be retrieved with Object.getPrototypeOf). The actual prototype of a constructor is Function.prototype since constructors are functions. Its prototype property will be the prototype of instances created through it but is not its own prototype.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论