object.getPrototypeOf函數(javascript)

返回對象的原型。函數

語法ui

Object.getPrototypeOf(object)

參數this

objectspa

  必需。引用原型的對象。prototype

返回值3d

  object 參數的原型。原型也是對象。code

異常對象

  若是 object 參數不是對象,則將引起 TypeError 異常。blog

function Pasta(grain, width) {
    this.grain = grain;
    this.width = width;
}

var spaghetti = new Pasta("wheat", 0.2);


var proto = Object.getPrototypeOf(spaghetti);

proto.foodgroup = "carbohydrates";
console.log(spaghetti.foodgroup + " "); //carbohydrates 

var result = (proto === Pasta.prototype);
console.log(result + " "); //true

var result = proto.isPrototypeOf(spaghetti);
console.log(result); //true

下面的示例使用 Object.getPrototypeOf 函數來驗證數據類型。get

var reg = /a/;
var result = (Object.getPrototypeOf(reg) === RegExp.prototype);
document.write(result + " "); //true

var err = new Error("an error");
var result = (Object.getPrototypeOf(err) === Error.prototype);
document.write(result);  //true
相關文章
相關標籤/搜索