了解原型、原型链、继承、constructor构造函数,原型链继承。

1.原型:
javascript规定,每一个函数都有一个prototype对象属性,指向另一个对象(原型链上面的)
prototype(对象属性)的所有属性和方法,都会被构造函数的实例继承.这意味着,我们可以把
那些不变(公用)的属性和方法直接定义在prototype对象属性上。
prototype就是调用构造函数所创建的那个实例对象的原型(proto)
prototype可以让所有对象实例共享它所包含的属性和方法.也就是说,不必在构造函数中定义对象
信息,而是可以直接将这些信息添加到原型中。

2.原型链:
javascript,每个对象都会在内部生成一个_proto_属性,当我们访问一个对象属性时,
如果这个对象不存在就会去_proto_指向的对象里面找,一层一层找下去,这就是javascript原型链的概念。js中所有的东西都是对象,所有的东西都由object衍生而来,即所有东西原型链的终点指向null。

3.constructor(构造函数):
在Javascript语言中,constructor属性是专门为function而设计的,它存在于每一个function的prototype属性中这个constructor保存了指向function的一个引用。

通俗解释:我们在每一个对象实例中可以访问构造函数的prototype所有拥有的全部属性和方法,就好像是实它们例自己的一样当然改实例也有一个constructor属性了(从prototype那里获得的)每一个对象实例都可以通过constructor(实例.constructor)对象访问它的构造函数。

4.继承:
一个子元素可以获取父类的所有属性和方法,称之为继承。

5.继承的方式:
原型链继承:
function Person(){
this.foot=2;
this.head=1;
}
function Student(name,no){
this.name=name;
this.no=no;
}
Student.prototype=new Person();
Student.prototype.constructor=Student;
var stu1=new Student(“张三”,“001”);
对上一种方式的改进:节省代码
function Person1() {}
Person1.prototype.foots=2;
Person1.prototype.eyes=2;
Person1.prototype.heads=1;
function Chinese2(name,age) {
this.name=name;
this.age=age;
}
Chinese2.prototype=new Person1(); //通过prototype属性对象 new实例共享
Chinese2.prototype=Person1.prototype;//对上一种方法的改进
Chinese2.prototype.constructor=Chinese2;//改进chinese2对Person1的实例//如果不改进就指向Person1构造函数了
Chinese2.prototype.foots=3;//1.通过prototype属性改变值
var chinese3=new Person1(“王五”,28);
console.log(chinese3.constructor);//实例指向chinese2构造函数
var person2=new Person1();
console.log(person2.foots);//2.Person1里面内容同样被继承修改

优点:效率比较高(不用执行和修改Person的实例)
缺点:Chinese.prototype和Person。prototype现在指向了同一个对象,任何对Chinese.prototype的
修改,都会反应到Person.prototype上。

constructor:代码:

<script>
    //constructor构造函数:
    function Person(name,age,job){
    
    
        this.name=name;
        this.age=age;
        this.job=job;
        this.sayName=function() {
    
    
            alert("my name is "+this.name);
        }
    }
    var person1=new Person("张三",24,"大学毕业");
    console.log(person1.constructor);//实例.constructor属性可以获取构造函数//Person
    person1.sayName();

</script>

JavaScript代码:

<script>
    function Person() {
    
    
        this.foots=2;
        this.eyes=2;
        this.headse=1;
    }
    function Chinese(name,age) {
    
    
        this.name=name;
        this.age=age;
    }
    //prototype属性
    Chinese.prototype=new Person();//通过chinese  new一个实例,这样就保存Person的实例继承
    Chinese.prototype.constructor=Chinese;//重新修改回Chinese实例的指向//如果不修改就会指向Person构造函数
    var chinese1=new Chinese("张三",18);
    console.log(chinese1.foots);//2
    console.log(chinese1.constructor);//指向Chinese构造函数

    //对上一种方法的改进
    function Person1() {
    
    }
    Person1.prototype.foots=2;
    Person1.prototype.eyes=2;
    Person1.prototype.heads=1;
    function Chinese2(name,age) {
    
    
        this.name=name;
        this.age=age;
    }
    Chinese2.prototype=new Person1();  //通过prototype属性对象 new实例共享
    Chinese2.prototype=Person1.prototype;//对上一种方法的改进
    Chinese2.prototype.constructor=Chinese2;//改进chinese2对Person1的实例//如果不改进就指向Person1构造函数了
    Chinese2.prototype.foots=3;//1.通过prototype属性改变值
    var chinese3=new Person1("王五",28);
    console.log(chinese3.constructor);//实例指向chinese2构造函数
    var person2=new Person1();
    console.log(person2.foots);//2.Person1里面内容同样被继承修改
</script>

猜你喜欢

转载自blog.csdn.net/weixin_46409887/article/details/105480260
今日推荐