function Person(name) { //構造函數
this.name = name;
}
Person.prototype.printName = function () { //原型對象
alert(this.name);
}
var person1 = new Person('Byron'); //實例化對象
console.log(person1.__proto__); //Person
console.log(person1.constructor); //本身試試看會是什麼吧
console.log(Person.prototype); //指向原型對象Person
var person2 = new Person('Frank');
複製代碼
// 構造函數表示方法1
// 不須要new生成實例
function Animal (name, energy) {
let animal = Object.create(Animal.prototype)
animal.name = name
animal.energy = energy
return animal
}
// 構造函數表示方法2
// 須要new生成實例
function Animal (name, energy) {
// const this = Object.create(Animal.prototype)
this.name = name
this.energy = energy
// return this
}
const leo = new Animal('Leo', 7)
const snoop = new Animal('Snoop', 10)
複製代碼
一般咱們使用構造函數2的方法來寫一個構造函數,當new 一個關鍵字時,let this = Object.create(Animal.prototype)、 return this這兩句代碼,會在引擎會隱式調用,而且建立的對象稱爲thisjavascript
調用構造函數,忘記new實例防錯html
// 警告
function Animal (name, energy) {
if (this instanceof Animal === false) {
console.warn('Forgot to call Animal with the new keyword')
}
this.name = name
this.energy = energy
}
// 生成實例
function Animal (name, energy) {
if (this instanceof Animal === false) {
return new Animal(name, energy)
}
this.name = name
this.energy = energy
}
複製代碼
function Animal (name, energy) {
this.name = name
this.energy = energy
}
Animal.prototype.eat = function (amount) {
console.log(`${this.name} is eating.`)
this.energy += amount
}
Animal.prototype.sleep = function (length) {
console.log(`${this.name} is sleeping.`)
this.energy += length
}
Animal.prototype.play = function (length) {
console.log(`${this.name} is playing.`)
this.energy -= length
}
const leo = new Animal('Leo', 7)
for(let key in leo) {
console.log(`Key: ${key}. Value: ${leo[key]}`)
}
複製代碼
for(let key in leo) {
if (leo.hasOwnProperty(key)) {
console.log(`Key: ${key}. Value: ${leo[key]}`)
}
}
複製代碼
function Animal (name, energy) {
this.name = name
this.energy = energy
}
Animal.prototype.eat = function (amount) {
console.log(`${this.name} is eating.`)
this.energy += amount
}
Animal.prototype.sleep = function (length) {
console.log(`${this.name} is sleeping.`)
this.energy += length
}
Animal.prototype.play = function (length) {
console.log(`${this.name} is playing.`)
this.energy -= length
}
const leo = new Animal('Leo', 7)
leo.hasOwnProperty('name') // true
leo.hasOwnProperty('energy') // true
leo.hasOwnProperty('eat') // false
leo.hasOwnProperty('sleep') // false
leo.hasOwnProperty('play') // false
複製代碼
function Animal (name, energy) {
this.name = name
this.energy = energy
}
function User () {}
const leo = new Animal('Leo', 7)
leo instanceof Animal // true
leo instanceof User // false
複製代碼
Object.create = function (objToDelegateTo) { }
複製代碼
Object.create = function (objToDelegateTo) {
function Fn(){}
Fn.prototype = objToDelegateTo
return new Fn()
}
複製代碼
var obj = {a: 1, b: ['red', 'blue']}
var objInstance1 = Object.create(obj)
var objInstance2 = Object.create(obj)
objInstance1.b.push('green')
objInstance2.b // ['red', 'blue', 'green']
// 對於基本類型會從新賦值
如:objInstance1.a = 3
console.log(objInstance1) // {a: 3}
複製代碼
function Animal() {
this.species = "動物";
}
function Cat(name, color) {
// this 指向Cat
Animal.apply(this, arguments);
this.name = name;
this.color = color;
}
// 將Cat的prototype對象指向一個Animal的實例,至關於徹底刪除了prototype 對象原先的值,而後賦予一個新值
Cat.prototype = new Animal();
// 任何一個prototype對象都有一個constructor屬性,指向它的構造函數。
// 若是沒有"Cat.prototype = new Animal();"這一行,Cat.prototype.constructor是指向Cat的;
// 加了這一行之後,Cat.prototype.constructor指向Animal。 
// 顯然會致使繼承鏈的紊亂(cat1明明是用構造函數Cat生成的),
// 所以咱們必須手動糾正,將Cat.prototype對象的constructor值改成Cat。
Cat.prototype.constructor = Cat;
var cat1 = new Cat("大毛", "黃色");
// console.log(cat1.species)
console.log(cat1)
複製代碼
function extend(Child, Parent) {
var F = function(){};
  F.prototype = Parent.prototype;
  Child.prototype = new F();
  Child.prototype.constructor = Child;
  Child.uber = Parent.prototype;
}
  
extend(Cat,Animal);
var cat1 = new Cat("大毛","黃色");
alert(cat1.species); // 動物
複製代碼