Does a new object in Javascript have a prototype property?

后端 未结 3 1481
忘掉有多难
忘掉有多难 2020-12-24 08:38

This is a purely trivial question for academic value:

If I create a new object, either by doing:

var o = { x:5, y:6 };

or



        
3条回答
  •  北海茫月
    2020-12-24 09:06

    JavaScript has two types of objects: function object and non-function object. Conceptually, all objects have a prototype (NOT A PROTOTYPE PROPERTY). Internally, JavaScript names an object's prototype as [[Prototype]].

    There are two approaches to get any object (including non-function object)'s [[prototype]]: the Object.getPrototypeOf() method and the __proto__ property. The __proto__ property is supported by many browsers and Node.js. It is to be standardized in ECMAScript 6.

    Only a function (a callable) object has the prototype property. This prototype property is a regular property that has no direct relationship with the function's own [[prototype]]. When used as a constructor ( after the new operator), the function's prototype property will be assigned to the [[Prototype]] of a newly created object. In a non-function object, the prototype property is undefined . For example,

    var objectOne = {x: 5}, objectTwo = Object.create({y: 6});
    

    Both objectOne and objectTwo are non-function objects therefore they don't have a prototype property.

提交回复
热议问题