原型
//Person.prototype 原型
Person.prototype.lastName = "wang"; //原型
Person.prototype.say = function(){
console.log("so cool")
}
对象里面有的就会调用自己的没有的就会调用原型上的;
function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
var person = new Person('lili',18,"girl");
原型的增删改查
Person.prototype.lastName = "wang";
function Person(name){
this.name = name;
this.age = 18;
}
var person = new Person("lili");
//person.lastNane = "lei";更改不了原型的属性只会在对象里添加lastName属性
//person.prototype.lastName = "lei"只有调用prototype才能更改/增加原型的属性
工厂模式
//工厂模式
Car.prototype.length = "10";
Car.prototype.height = "2";
Car.prototype.weight = "2t";
function Car(name,color){
this.name = name;
this.color = color;
}
var person = new Person("leiyu","red");
var person2 = new Person("lili","black")