JavaScript中的constructor、instanceof、isPrototypeOf、typeof以及hasOwnProperty

 

一、hasOwnProperty和in

先來理解hasOwnProperty方法。這個方法是用來檢查對象的非原型鏈屬性,換句話說,也就是檢查對象中用戶自定義的屬性,並且這些屬性不是定義在prototype上的。經過下面的代碼進行理解:函數

var myFunc=function(){
    this.foo='value';     
};
myFunc.prototype.ok='ok';
thefunc=new myFunc();
console.log(
    thefunc.hasOwnProperty('foo'),//true
    thefunc.hasOwnProperty('ok'),//false
    myFunc.hasOwnProperty('foo'),//false
    myFunc.prototype.hasOwnProperty('ok'),//true這裏有點特殊
    'ok' in thefunc,//true
    'foo' in thefunc,//true
    'ok' in myFunc.prototype,//true
    'ok' in myFunc//false
);

在上面的代碼中,foo是用戶自定義的非原型屬性,而ok則是定義在prototype上的原型屬性,因此thefunc.hasOwnProperty('foo')的值爲true,thefunc.hasOwnProperty('ok')的值爲false。測試

JavaScript語言中還有一個in操做符,用來檢查一個對象的屬性,包括來自原型鏈的屬性。this

 

二、constructor

每個JavaScript函數(ECMAScript 5中的Function.bind()返回的函數除外)都自動擁有一個prototype屬性。這個屬性的值是一個對象,這個對象包含惟一一個不可枚舉的屬性constructor。構造函數實例都擁有指向其構造函數的constructor屬性。constructor屬性的值是一個函數對象:spa

var F=function(){};//這是一個函數對象
var P=F.prototype;//這是F相關聯的原型對象
var C=P.constructor;//這是與原型相關聯的函數
C===F;//true,對於任意函數F.prototype.constructor===F
//摘自JavaScript權威指南

在這裏插入第一節裏的代碼,並擴展了一些,但願可以解釋清楚構造函數和原型的關係:prototype

var myFunc=function(){
    this.foo='value';     
};
myFunc.prototype.ok='ok';
thefunc=new myFunc();
console.log(
    thefunc.constructor===myFunc, //true   
    myFunc.constructor===Function,//true
    typeof thefunc,//object
    myFunc.prototype//Object(){ok='ok'}
);

三、instanceof和isPrototypeOf

instanceof是JavaScript語言中的一種運算符。左操做數是待檢測其類的對象,右操做數是定義類的構造函數。所以,沒有使用構造函數的字面量形式定義的對象使用instanceof運算符時,沒法找到其構造函數。code

isPrototypeOf是用來判斷要檢查其原型鏈的對象是否存在於指定對象實例中,是則返回true,不然返回false。對象

var myFunc=function(){
    this.foo='value';     
};
myFunc.prototype.ok='ok';
thefunc=new myFunc();
console.log(
    thefunc instanceof myFunc,//true
    myFunc instanceof Function,//true
    myFunc.prototype.isPrototypeOf(thefunc),//true
    Function.prototype.isPrototypeOf(myFunc),//true     
    myFunc.prototype,//Object(){ok='ok'}
    typeof thefunc,//object
    thefunc.prototype//undefined,這裏沒搞懂
);

四、typeof

typeof是一元運算符,放在操做數的前面,操做數能夠是任意類型。返回值爲表示操做數類型的一個字符串。typeof是用來鑑別原始類型的最佳方法。blog

 

 

 

下面是以上幾個操做的測試用例:ip

var myNumber = new Number('23');
var myNumberL = 23; // literal shorthand
var myString = new String('male');
var myStringL = 'male'; // literal shorthand
var myBoolean = new Boolean('true');
var myBooleanL = true; // literal shorthand
var myObject = new Object();
var myObjectL = {}; // literal shorthand
var myArray = new Array();
var myArrayL = []; // literal shorthand
var myFunction = new Function();
var myFunctionL = function() {}; // literal shorthand
var myDate = new Date();
var myRegExp = new RegExp('/./');
var myRegExpL = /./; // literal shorthand
var myError = new Error();

console.log( // all of these return true
    myNumber.constructor === Number,
    myNumberL.constructor === Number,
    myString.constructor === String,
    myStringL.constructor === String,
    myBoolean.constructor === Boolean,
    myBooleanL.constructor === Boolean,
    myObject.constructor === Object,
    myObjectL.constructor === Object,
    myArray.constructor === Array,
    myArrayL.constructor === Array,
    myFunction.constructor === Function,
    myFunctionL.constructor === Function,
    myDate.constructor === Date,
    myRegExp.constructor === RegExp,
    myRegExpL.constructor === RegExp,
    myError.constructor === Error
    
);
console.log( //others return true
    myNumber instanceof Number,
    myNumberL instanceof Number,//false
    myString instanceof String,
    myStringL instanceof String,//false
    myBoolean instanceof Boolean,
    myBooleanL instanceof Boolean,//false
    myObject instanceof Object,
    myObjectL instanceof Object,
    myArray instanceof Array,
    myArrayL instanceof Array,
    myFunction instanceof Function,
    myFunctionL instanceof Function,
    myDate instanceof Date,
    myRegExp instanceof RegExp,
    myRegExpL instanceof RegExp,
    myError instanceof Error
);
console.log( //others return true
    Number.prototype.isPrototypeOf(myNumber),
    Number.prototype.isPrototypeOf(myNumberL),//false
    String.prototype.isPrototypeOf(myString),
    String.prototype.isPrototypeOf(myStringL),//false
    Boolean.prototype.isPrototypeOf(myBoolean),
    Boolean.prototype.isPrototypeOf(myBooleanL),//false
    Object.prototype.isPrototypeOf(myObject),
    Object.prototype.isPrototypeOf(myObjectL),
    Array.prototype.isPrototypeOf(myArray),
    Array.prototype.isPrototypeOf(myArrayL),
    Function.prototype.isPrototypeOf(myFunction),
    Function.prototype.isPrototypeOf(myFunctionL),
    Date.prototype.isPrototypeOf(myDate),
    RegExp.prototype.isPrototypeOf(myRegExp),
    RegExp.prototype.isPrototypeOf(myRegExpL),
    Error.prototype.isPrototypeOf(myError)
);
console.log(
    typeof myNumber,//object
    typeof myNumberL,//number
    typeof myString,//object
    typeof myStringL,//string
    typeof myBoolean,//object
    typeof myBooleanL,//boolean
    typeof myObject,//object
    typeof myObjectL,//object
    typeof myArray,//object
    typeof myArrayL,//object
    typeof myFunction,//function
    typeof myFunctionL,//function
    typeof myDate,////object
    typeof myRegExp,//object
    typeof myRegExpL,//object
    typeof myError//object
);
相關文章
相關標籤/搜索