首页 > 移动平台 > 详细

渡一——11-1原型,原型链,call/apply(上)

时间:2021-09-21 20:21:35      阅读:28      评论:0      收藏:0      [点我收藏+]

原型
1.定义:原型是fn对象的一个属性,它定义了构造函数制造出对象的公共祖先。通过该构造函数产生的对象,可以继承该原型的属性和方法。原型也是对象。
2.利用原型特点和概念,可以提取共有属性
3.对象如何查看原型——隐匿属性__proto__
4.对象如何查看对象的构造函数 ——constructor

 

构造函数是大驼峰式

Person.prototype    ——原型
Person.prototype={} ——祖先
Person.prototype.name = "hehe";
function Person(){
    //同样的属性和方法

}
var person = new Person();
person.name //hehe
person1.name //hehe

 

原型的特点

1.自己有的用自己的,不用父亲的

Person.prototype    ——原型
Person.prototype={} ——祖先
Person.prototype.Lastname = "Deng";
Person.prototype.say = function(){console.log(‘hehe‘)}
function Person(name,age,sex){
    //同样的属性和方法
    // this.Lastname = "newName"
    this.name = name;
    this.age = age;
    this.sex = sex;
}
var person = new Person(‘xumin‘,36,‘male‘);
person.name //newName
// person1.name //hehe

弊端 代码冗余,耦合;共有的放原型里,只加载一次

Car.prototype.height = 1400;
Car.prototype.lang = 4900;
Car.prototype.carName = "BMW";

function Car(color,owner){
    /*this.owner = owner;
    this.color = color;
    this.carName = "BMW";
    this.height = 1400;*/

    this.owner = owner;
    this.color = color;

}

var car = new Car(‘red‘,‘prof.ji‘);
var car = new Car(‘green‘,‘LaoDeng‘);

简化版本的prototype

Car.prototype={
    height:1400,
    lang:4900,
    carName:"BMW"
}

Car.prototype.height = 1400;
Car.prototype.lang = 4900;
Car.prototype.carName = "BMW";

function Car(color,owner){

}

var car = new Car();
car.carName //BMW

 

原型的增删改查 对象无法操作原型,只能用prototype来操作

Person.prototype.lastName = "Deng";
function Person(name){
    this.name = name;
}
var person = new Person(‘xuming‘);
person.lastName = "James" //这里是对象自身的属性,和原型没关系
person.prototype.lastName = "James" //这才是修改原型属性

delete person.name;
delete.person.lastName //true 并没有真正删除

constructor 指针,指向构造函数

Car.prototype.abc = "123"
function Car(color,owner){

}

var car = new Car();
car.constructor //Car 返回构造函数
Car.prototype //返回系统自带的

//更改指针
function Person(){}
Car.ptototype = {
    car.constructor : Person
}

 

渡一——11-1原型,原型链,call/apply(上)

原文:https://www.cnblogs.com/lisa2544/p/15307198.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!