关于javascript:面向对象原型链继承

2次阅读

共计 669 个字符,预计需要花费 2 分钟才能阅读完成。

一、创建对象的几种办法

// 1、字面量
var o1 = {name:'lihaixing'};
var o11 = new Object({name:'lihaixing'});
// 2、构造函数
var M = function(){this.name='haixing'};
var o2 = new M();
// 3、基于原型
var P = {name:'haixing'};
var o3 = Object.create(P);
var o33 = Object.create(null);

二、原型链

var M = function () {
    this.name = 'haixing';
    // return {}};
var o2 = new M();
console.log(M.prototype.constructor === M); // true
console.log(M.prototype === o2.__proto__); // true
console.log(M.prototype.__proto__ === Object.prototype); // true
console.log(M.__proto__ === Function.prototype); // true

console.log(o2 instanceof M); // true 
console.log(M.prototype instanceof Object); // true 
console.log(o2 instanceof Object); // true  只有一条链上就能够
console.log(Object.prototype.__proto__===null); // true
正文完
 0