【JavaScript 6连载】六、认识原型

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>06-认识原型</title>
<script>


function Person(name,sex,age){
this.name = name;
this.sex = sex;
this.age = age;
// 下面这种方式不能达到方法的共享
// this.study = function(){
// console.log(this.name + '好好学习');
// }
}

Person.prototype.study = function(){
console.log(this.name + '好好学习');
};

var p1 = new Person('张三','b',18);
console.log(p1);
var p2 = new Person('李四','b',19);
console.log(p2);

// 在实例上动态添加属性,会把原型对象上相同的属性给屏蔽掉
p2.study = function(){
console.log('别打扰我,我要学习');
};

// 删除p2中的age

// delete p2.age;

// 判断某个属性是否在一个实例中(属性必须在当前的实例中,而不是在实例的构造函数的原型中)
if (p2.hasOwnProperty('age')) {
console.log(p2.age);
}
if (p2.hasOwnProperty('study')) {
p2.study();
}


</script>
</head>
<body>

</body>
</html>

实例二:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>创建对象</title>

</head>
<body>
</body>
<script>

// 一个正常的构造器
function Person(name, age) {
this.home = "保护地球,人人有责!";
this.name = name;
this.age = age;
this.desc = function(){
console.log(this.name+" "+this.age+" "+this.home);
}
}

function Student(s){
this.school = s;
}

// 原型继承,如果一个构造器需要需要创建一个有继承的对象时,可以设置构造器的 prototype ,将其指定为一个 作为原型 的对象
Student.prototype = new Person("xxx", 1);
// var protoObj = new Person("xxx", 1);
// Student.prototype = protoObj;
// 子对象的构造器.prototype = new 原型对象的构造器(var1, var2....)

var st = new Student("青云学院");
console.log(st.school);
st.desc();
var st1 = new Student("斯坦福大学");
console.log(st1.school);
st1.desc();
// protoObj.home = "保护地球,从小做起";
Student.prototype.home = "保护地球,从小做起";
st.desc();
st1.desc();

// st.home = "河南";
// st1.home = "荷兰";
// st.desc();
// st1.desc();

</script>
</html>

实例三:

function Person(name, age) {
this.home = "保护地球,人人有责!";
this.name = name;
this.age = age;
this.desc = function(){
console.log(this.name+" "+this.age+" "+this.home);
}
}

// 注意:这里的 sex和study是在追加新的属性和方法之前就调用的,是不能生效的
// var p0 = new Person("小红", 20);
// console.log(p0.sex);
// p0.study();

// Person作为一个正常的构造器也有自己的原型,在没有明确指定 原型的时候 它的原型是一个抽象的 Object 对象,利用这样的机制可以在不修改或者不明确源代码时为该构造器追加新的功能
Person.prototype.sex = "男";
Person.prototype.study = function(){
console.log(this.name+"学习很努力!");
}

var p = new Person("小红", 20);
p.study();

实例四:

function Parent(name,age,sex){
this.home = "保护地球,人人有责!";
this.name = name;
this.age = age;
this.sex = sex;
this.desc = function(){
console.log(this.name+" "+this.age+" "+this.home);
}
}

// 给parent的原型上增加一个方法;
Parent.prototype.say = function(){
console.log(this.name + "说:" + "好好学习吧!")
}

// 创建一个子类构造函数:
function Child(name,age,sex,work){
Parent.call(this,name,age,sex);
this.work = work;
}


// 原型:
Child.prototype = new Parent();
// var protoObj = new Parent();
// Child.prototype = protoObj;


// 给child的原型上增加一个方法;
Child.prototype.show = function(){
console.log(this.name + "的工作是" + this.work );
}


// var parent = new Parent();
// console.log(parent);
// console.log(parent.age);
var child = new Child("小红",18,"girl","designer");
console.log(child);

child.say();
child.show();
// Child.prototype.home = "我爱我家";
// console.log(child.home);

var child1 = new Child("小花",23,"girl","teacher");
console.log(child1);
child1.say();
child1.show();
child1.show = function(){
console.log(this.age +"岁的"+ this.name + "的工作是" + this.work );
}
child1.show();
child1.home = "河南郑州";
console.log(child1.home);
console.log(child1);
alert(child1.constructor == Parent)


Child.prototype.home = "四川";
console.log(child.home);
child.desc()

猜你喜欢

转载自www.cnblogs.com/7qin/p/10226479.html