hasOwnProperty,instanceof,isPrototypeof

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	
</body>
<script>
	//hasOwnProperty:判斷一個具體對象的某個屬性是屬於構造函數自身的,還是構造函數原型上的,如果是構造函數中的,返回值爲true,是原型中的就返回false


	function A(){
		this.name="rose";
	}
	A.prototype.age=10;

	var a = new A();
	console.log(a.hasOwnProperty("name"));//true
	console.log(a.hasOwnProperty("age"));//false

	function B(){}
	B.prototype=a;
	var b = new B();

	//instanceof:運算符:判斷運算符左邊的對象是否由右邊的構造函數創建
	//判斷 右邊的構造函數是否存在於左邊對象的原型鏈上
	if(a instanceof A){
		console.log(true);//true
	}else{
		console.log(false);
	}

	//isPrototypeof:左邊的對象是否存在於右邊對象的原型鏈上,此刻原型鏈上判斷的一般都是xxx.prototype
	console.log(Object.prototype.isPrototypeOf(b));//true




</script>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章