标签:
1.普通对象都有_proto_
2.函数都有prototype
3._proto_是来源于prototype
4.如自己没有该属性,则沿着_proto_往上找。
5.组合继承
function Father(name) {
this.name = name;
}
Father.prototype.lookBook = function() {
console.info(this.name + "在看书");
}
function Son(name, age) {
//继承属性,继承不了prototype的东西
Father.apply(this, [name, age]);//或call
this.hobby = "打游戏";//这个写在哪里都无所谓
}
//继承方法method
Son.prototype = new Father();
//新的方法必须写在继承之后
Son.prototype.playPiano = function() {
}
var son = new Son("李四", 22);
son.lookBook();
6.私有化属性(了解)
function Student(name) {
var name = name;//私有化属性
this.study = function() {
//不能使用this.name
console.info(name + "说:今天好热!");
}
}
var stu = new Student("龙美丽");
//只能通过方法去使用
stu.study();
//直接点(.)是不能用的
console.info(stu.name);
7.JSON对象(套路)
var stu = {
"name": "xx",
age: 16,
classmate: {
name: "xx",
sex: "lady"
}
}
console.info(stu.age);
stu.boyFriend = "xx";
console.info(stu.boyFriend);
console.info(stu.name);
console.info(stu.classmate.name);
8.构造函数
<script>
/*
学生
- 首字母大写
- 函数内部使用this
- 除了对属性进行封装私有化
- 创建对象要用new
*/
function Student(name) {
this.name = name;
this.playComputerGame = function() {
};
this.sleepy = function() {
console.info(this.name);
};
}
function Cat() {
}
var stu = new Student("张三");
var stu1 = new Student("李四");
//stu这个对象是属于学生类
console.info(stu instanceof Student);
//而stu这个对象不属于猫类
console.info(stu instanceof Cat);
//因为所有的对象都是属于Ojbect类
console.info(stu instanceof Object);
</script>
标签:
原文地址:http://www.cnblogs.com/weixiang1012/p/5440835.html