//原型链/*只要是对象就有原型, 并且原型也是对象,因此只要定义了一个对象, 那么就可以找到他的原型,如此反复, 就可以构成一个对象的序列, 这个结构就被成为原型链*/var Obj = function(){};var o = new Obj();o.__proto__ === Obj.prototype; //=> trueo.__proto__.constructor === Obj; //=> trueObj.__proto__ === Function.prototype; //=> trueObj.__proto__.constructor === Function; //=> trueFunction.__proto__ === Function.prototype; //=> trueObject.__proto__ === Object.prototype; //=> falseObject.__proto__ === Function.prototype; //=> trueFunction.__proto__.constructor === Function;//=> trueFunction.__proto__.__proto__; //=> {}Function.__proto__.__proto__ === o.__proto__.__proto__; //=> trueo.__proto__.__proto__.__proto__ === null; //=> true
//call apply 继承/*父类*/function Parent(name, age) {this.name = name;this.age = age;}/*子类*/function Child(id) {this.id = id;//Parent.apply(this,["Sure",13])Parent.call(this, "Sure", 13);//这个时候的Parent中的this已经被Child所代替}var child = new Child("01");console.log(child.name)
//深拷贝function deepClone(obj) { //递归拷贝if(obj == null)return null;if(typeof obj !== 'object')return objlet t = new obj.constructor();for(let key in obj) {t[key] = deepClone(obj[key]);}return t;}
//自己写的深拷贝let deepCopy = (data) => {let obj;if (typeof (data) != "object" || data == null) {obj = data;} else if (Array.isArray(data)) {obj = [];data.forEach(item => {obj[key] = deepCopy(item);})} else {obj = {};for (key in data) {obj[key] = deepCopy(data[key]);}}return obj;}