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); // 动物
复制代码