Person.prototype.name = '小明'
Person.prototype.say = function(){
console.log('hello word')
}
function Person(){}
var person = new Person()
console.log(person.name) //小明
person.say() // hello word
function Car(color, owner){
this.owner = owner
this.color = color
this.height = 1400
this.lang = 4900
}
//可以把他们共有的属性提取到prototype上
Car.prototype.height= 1400
Car.prototype.lang = 4900
function Car(color, owner){
this.color = color
this.owner = owner
}
function Person(){}
Car.prototype = {
constructor: Person
}
function Car(){}
var car = new Car()
console.log(car.constructor) //Person(){}
Person.prototype.name = 'abc'
function Person(){
var this = {
__proto__:Person.prototype
}
}
var person = new Person();
console.log(person.name) //abc
2.引用值和原始值的区别
Person.prototype.name = 'sunny'
function Person() {}
var person = new Person()
Person.prototype.name = 'cherry'
console.log(person.name) //cherry
Person.prototype.name = 'sunny'
function Person() {}
var person = new Person()
Person.prototype= { name:'cherry'}
console.log(person.name) //sunny
Person.prototype.name = 'sunny'
function Person() {}
Person.prototype.name = 'cherry'
var person = new Person()
console.log(person.name) //cherry
3.绝大多数对象的最终都会继承自Object.prototype
obj.create(null) //prototype : no
function Person(name, age, sex){
this.name = name
this.age = age
this.sex = sex
}
function Student(name,age,sex,grade,tel){
Person.call(this, name, age, sex)
this.grade = grade
this.tel = tel
}