前端基础——原型、instanceof

版权声明:转载请注明出处! https://blog.csdn.net/qq_42303885/article/details/88063528

文章早就有了,现在制作一张图放上来。
在这里插入图片描述
什么是原型对象
当我们声明一个函数时A,浏览器会在内存中创建一个对象B,每个函数都有一个默认的prototype属性指向这个对象B,对象B默认有个constructor属性指回这个函数,这个对象B就是函数A的原型对象。
constructor不是实例化对象的自有属性,而是原型属性,因为原型对象的constructor指向构造函数,所以实例化对象的constructor属性指向构造函数。

接下来看上面这张图
1、 每个对象都有__proto__属性,但只有函数才有prototype属性。函数也是对象,所以函数也有__proto__属性。
2、 对象的__proto__属性指向创建该对象的构造函数的原型对象。

		function Person(name){
			this.name=name;
		}
		var student=new Person("xiaopeng");
		console.log(student.__proto__===Person.prototype);//true

3、所有构造函数的constructor都指向Function,包括Function和Object。所以,所有构造函数的__proto__都指向Function.prototype。

		console.log(Person.__proto__===Function.prototype);//true
		console.log(Function.__proto__===Function.prototype);//true
		console.log(Object.__proto__===Function.prototype);//true

4、所有原型对象的__proto__属性都指向Object.prototype,除了Object.prototype,Object.prototype. __proto__指向null。

		console.log(Person.prototype.__proto__===Object.prototype);//true
		console.log(Function.prototype.__proto__===Object.prototype);//true
		console.log(Object.prototype.__proto__===null);//true

5、 Function.prototype比较例外,它是函数对象,而且它虽然是函数,但是没有prototype属性。

		console.log(typeof(Person.prototype));//object
		console.log(typeof(Function.prototype));//function
		console.log(typeof(Object.prototype));//object
		console.log(Function.prototype.prototype);//undefined

6、 Object是JavaScript中所有对象的父级对象。原型链,在本级中找不到相应的属性或方法,再通过__proto__去原型对象中查找,Object是最高级的父级对象,即直到搜索到Object.prototype为止。如果本级中找到了,就不会再去原型中查找

instanceof
L instanceof R ,是看右边是否是在左边的原型链上,即L.proto.proto … === R.prototype 是否成立。

		console.log(Function instanceof Function);//true
		console.log(Object instanceof Function);//true
		console.log(Object instanceof Object);//true
		console.log(Function instanceof Object);//true

可以解释为:

		console.log(Function.__proto__===Function.prototype);//true
		console.log(Object.__proto__===Function.prototype);//true
		console.log(Object.__proto__.__proto__===Object.prototype);//true
		console.log(Function.__proto__.__proto__===Object.prototype);//true

另外

		console.log(Number instanceof Number);//false
		console.log(String instanceof String);//false
		console.log(Number instanceof Function);//true
		console.log(String instanceof Function);//true

new之后,实例化对象的__proto__指向构造函数的prototype,之后实例化对象就与构造函数没有什么关系了

	    function Person () {
	        this.name = 'John';
	    }
	    var person = new Person();
	    Person.prototype.say = function() {
	        console.log('Hello,' + this.name);
	    };
	    person.say();//Hello,John

	    function Person () {
	        this.name = 'John';
	    }
	    var person = new Person();
	    Person.prototype = {
	        say: function() {
	            console.log('Hello,' + this.name);
	        }
	    };
	    person.say();//person.say is not a function

       function Person () {
	        this.name = 'John';
	    }
	    Person.prototype = {
	        say: function() {
	            console.log('Hello,' + this.name);
	        }
	    };
	    var person = new Person();
	    person.say();//Hello,John

猜你喜欢

转载自blog.csdn.net/qq_42303885/article/details/88063528