image.png
    原型链的顶端:
    Object.prototype,不是Object构造函数

    1. Professor.prototype.tSkill = 'JAVA';
    2. function Professor(){}
    3. var professor = new Professor();
    4. Teacher.prototype = professor;
    5. function Teacher(){
    6. this.mSkill = 'JS';
    7. this.success = {
    8. alibaba: '28',
    9. tencent: '30'
    10. }
    11. }
    12. var teacher = new Teacher();
    13. Student.prototype = teacher; // 引用值
    14. function Student(){
    15. this.pSkill = 'HTML';
    16. }
    17. var student = new Student();
    18. student.success.baidu = '100';
    19. console.log(teacher,student)

    引用值:
    image.png
    原始值:
    image.png
    image.png
    this指向car对象
    普通函数不写返回值,默认返回undefined
    构造函数被实例化后返回this
    image.png

    1. var obj1 = {}
    2. var obj2 = new Object(); // 与obj1没什么不同
    3. function Obj(){}
    4. var obj3 = new Obj();

    image.png
    原型的原型由new Object构造出来的

    1. Object.create(对象, null) 创建对象
    2. 可自定义原型对象
    3. var test = Object.create(对象, null) // 这里对象会作为test的原型对象

    image.png
    new Obj() 实例化obj2,调用构造函数Obj的初始化属性和方法,然后指定实例对象的原型
    image.png
    Object.create()可把其他对象作为自己的原型对象,继承关系

    不是所有的对象都继承于Object.prototype ——> Object.create(null);

    1. var obj = Object.create(null);
    2. obj.num =1;
    3. consolg.loe(obj); // {num: 1} 此时没有原型__proto__
    4. var obj = Object.create(null);
    5. obj.num =1;
    6. var obj1 = {
    7. count: 2
    8. }
    9. obj.__proto__ = obj1; // 可以更改,不可以制造,无效果
    10. console.log(obj1.count); // undefined

    image.png

    1. // undefined(null).toString(); 报错 undefined、null不经过包装类,也没有原型
    2. // 原始值是没有属性的
    3. var num = 1;
    4. num.toString();
    5. // new Number(1) --> toString()
    6. var num2 = new Number(num);
    7. console.log(num2.toString());// 调用的是Number上的toString方法,而不是原型Object上的toString
    1. var num = 1;
    2. var obj = {};
    3. var obj2 = Object.create(null);
    4. document.write(num); // 1
    5. document.write(obj); // [Object, Object] 隐式转换 转换成string(toString) 有原型
    6. document.write(obj2);// 报错 没有原型

    image.png
    image.png
    两者toString()打印出来不一样,说明原型方法重写了,Number.prototype对toString方法重写

    1. //call、apply
    2. function Car(brand,color){
    3. this.brand = brand;
    4. this.color = color;
    5. }
    6. var newCar = {};
    7. // 改变this指向
    8. Car.call(newCar, 'Benz', 'red');
    9. Car.apply(newCar, ['Benz', 'red']);

    image.png
    功能、分类汇总