console
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.say = function() {
console.log('我叫' + this.name);
}
//创建一个对象
function Coder(name, age, job) {
//Person(name,age); 直接调用函数的话,this指向了window,就会有问题
Person.call(this, name, age);
this.job = job;
}
/*
* 用这种方法的话
* 1、复制了一个对象的引用,如果改一个话,两个都会变
* 2、他们两个的构造函数都会指向同一个构造函数
*/
/*Coder.prototype=Person.prototype;
Coder.prototype.say=function(){
console.log(1);
}*/
//这种继承方法是可以的,但是如果方法有特别多的情况下就会麻烦了
Coder.prototype.say = Person.prototype.say;
Coder.prototype.say = function() {
console.log(1);
}
var p1 = new Person('kaivon');
var c1 = new Coder('kaivon', 18, '前端');
//c1.say();
//p1.say();
//console.log(c1 instanceof Coder);
//console.log(c1.constructor);
function Ninja() {
this.swung = true;
}
const ninja1 = new Ninja();
Ninja.prototype.swingSword = function() {
return this.swung;
};
//原型可以修改
Ninja.prototype = {
pierce: function() {
return true;
}
}
const ninja2 = new Ninja();
console.log(ninja2.constructor);
console.log(ninja1.constructor);
<label for="">
</label>