getPrototypeOf的实例

getPrototypeOf获取对象的原型链

by kixi

HTML

getPrototypeOf的作用是取得对象的原型链

CSS

/*
清晰网
kixi.com.cn
从javascript开始
*/

JavaScript

// Create a constructor function.
function Pasta(grain, width) {
    this.grain = grain;
    this.width = width;
}
// Create an object from the pasta constructor.
var spaghetti = new Pasta("wheat", 0.2);

// Obtain the prototype from the object.
var proto = Object.getPrototypeOf(spaghetti);

// Add a property to the prototype and validate that
// the original object has the property.
proto.foodgroup = "carbohydrates";
alert(spaghetti.foodgroup + " ");

// Verify that the prototype obtained from the object
// is the same as the prototype of the constructor.
var result = (proto === Pasta.prototype);
alert(result + " ");

// Verify that prototype obtained from the object
// is a prototype of the original object.
var result = proto.isPrototypeOf(spaghetti);
alert(result);