js原型链和继承
原型链的理解
var obj = {
name: 'Fhup',
age: 18
}
console.log(obj.__proto__)
obj.__proto__ = {
}
obj.__proto__.__proto__ = {
}
obj.__proto__.__proto__.__proto__ = {
address: '北京市'
}
console.log(obj.address)
函数与对象的原型差别
function foo() {
}
console.log(foo.prototype.constructor)
console.log(foo.__proto__.constructor)
console.log(foo.prototype === foo.__proto__)
console.log('--------------------')
var obj = {
}
console.log(obj.__proto__)
console.log(obj.__proto__ === Object.prototype)
console.log('````````````````');
console.log(Object.__proto__)
console.log(Object.__proto__.constructor)
console.log(Object.prototype.constructor)
console.log(Object.__proto__ === Object.prototype)
console.log(Object.__proto__.__proto__)
顶层原型是什么
var obj = {
}
console.log(obj.__proto__)
console.log(obj.__proto__.__proto__)
顶层原型来自哪里
var obj = new Object()
console.log(obj.__proto__)
console.log(Object.prototype)
console.log(obj.__proto__ === Object.prototype)
console.log(Object)
console.log(Object.prototype.constructor)
console.log(Object.getOwnPropertyDescriptors(Object.prototype));

Person构造函数原型
function Person() {
}
console.log(Person.prototype)
console.log(Person.prototype.__proto__)
console.log(Object.getOwnPropertyDescriptors(Person.prototype));
继承-原型链的继承方案
function Person() {
this.name = "Fhup"
this.friends = []
}
Person.prototype.eating = function() {
console.log(this.name + " eating~")
}
function Student() {
this.sno = 001
}
var p = new Person()
Student.prototype = p
Student.prototype.studying = function() {
console.log(this.name + " studying~")
}
var stu = new Student()
var stu1 = new Student()
var stu2 = new Student()
stu1.name = "kobe"
console.log(stu2.name)
stu1.friends.push("kobe")
console.log(stu1.friends)
console.log(stu2.friends)
var stu3 = new Student("lilei", 112)
继承-借用构造函数方案
function Person(name, age, friends) {
this.name = name
this.age = age
this.friends = friends
}
Person.prototype.eating = function() {
console.log(this.name + " eating~")
}
function Student(name, age, friends, sno) {
Person.call(this, name, age, friends)
this.sno = sno
}
var p = new Person()
Student.prototype = p
Student.prototype.studying = function() {
console.log(this.name + " studying~")
}
继承-父类原型赋值给子类
function Person(name, age, friends) {
this.name = name
this.age = age
this.friends = friends
}
Person.prototype.eating = function() {
console.log(this.name + " eating~")
}
function Student(name, age, friends, sno) {
Person.call(this, name, age, friends)
this.sno = sno
}
Student.prototype = Person.prototype
Student.prototype.studying = function() {
console.log(this.name + " studying~")
}
var stu = new Student("why", 18, ["kobe"], 111)
console.log(stu)
stu.eating()
继承-原型式继承-对象
var obj = {
name: 'Fhup',
age: 18
}
function createObject(o) {
var newobj = {}
Object.setPrototypeOf(newobj, o)
return newobj
}
function createObject2(o) {
function Fn() {}
Fn.prototype = o
var newobj = new Fn()
return newobj
}
var info = Object.create(obj)
console.log(info.__proto__)
继承-寄生式继承-对象
var personObj = {
running: function() {
console.log("running")
}
}
function createStudent(name) {
var stu = Object.create(personObj)
stu.name = name
stu.studying = function() {
console.log("studying~")
}
return stu
}
var stuObj = createStudent("why")
var stuObj1 = createStudent("kobe")
var stuObj2 = createStudent("james")
继承-寄生组合式继承(最终)
function createObject(o) {
function Fn() {}
Fn.prototype = o
return new Fn()
}
function inheritPrototype(SubType, SuperType) {
SubType.prototype = createObject(SuperType.prototype)
Object.defineProperty(SubType.prototype, 'constructor', {
writable: true,
configurable: true,
enumerable: false,
value: SubType
})
}
function Person(name, age, sex) {
this.name = name
this.age = age
this.sex = sex
}
Person.prototype.running = function() {
console.log(this.name + ' running~');
}
function Student(name, age, sex, address) {
Person.call(this, name, age, sex)
this.address = address
}
inheritPrototype(Student, Person)
Student.prototype.say = function() {
console.log(this.name + ' saying~');
}
var stu = new Student('Fhup', 18, '男', '北京市')
console.log(stu);
stu.say()
stu.running()
判断方法的补充
var obj = {
name: 'Fhup'
}
var info = Object.create(obj, {
address: {
value: '北京市',
enumerable: true
}
})
instanceof的判断
function Person() {
}
function Student() {
}
Student.prototype = Object.create(Person.prototype)
Student.prototype.constructor = Student
var stu = new Student()
console.log(stu)
console.log(stu instanceof Student)
console.log(stu instanceof Person)
console.log(stu instanceof Object)
isPrototypeOf的判断
function Person() {
}
var p = new Person()
console.log(p instanceof Person)
console.log(Person.prototype.isPrototypeOf(p))
var obj = {
name: "why",
age: 18
}
var info = Object.create(obj)
console.log(obj.isPrototypeOf(info))
对象-函数-原型之间的关系
var obj = {
name: "why"
}
console.log(obj.__proto__)
function Foo() {
}
console.log(Foo.prototype === Foo.__proto__)
console.log(Foo.prototype.constructor)
console.log(Foo.__proto__.constructor)
console.log(Function.prototype === Function.__proto__)
var foo1 = new Foo()
var obj1 = new Object()
console.log(Object.getOwnPropertyDescriptors(Function.__proto__))
