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