面向对象分为‘类’和‘实例’
什么是类?类是对象的类型模版;例如Student,我们可以把它当做学生类型;它不具体代表某个学生;
什么是实例?实例是根据类型模版生成的对象;利用Student这个类实例出的具体的学生;每个实例都代表一个具体的学生;他们都属于Student这个类型;
三大特征:
封装:根据客观事物抽象成类(例:学生类,由N个不同学生组成)
继承:类型模版A可继承类型模版B的所有属性,方法;
多态:多种状态,比如学生类,根据实例传参的不同,它可以是xiaoming xiaohua 或者xiaohong;
原型继承:
1:根据call()方法与apply()区别,apply必须穿数组([x,y])不然会报typerror
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>继承</title>
<script type="text/javascript">
function Student(props) {
this.name = props.name
}
Student.prototype.run = function () {
console.log(this,'this')
console.log(this.name,'is run')
}
function Xiaohua(props) {
Student.call(this,props) //call方法调用student函数,并让student函数的this对象替换添加到当前函数
this.grade = props.grade || 1;
}
function F() { //道格拉斯方式更改原型链,通过中间函数的方法继承指定构造函数
}
F.prototype = Student.prototype; //先F原型指向Student原型
Xiaohua.prototype = new F(); //新构造函数指向F;达到原型链正确的目的
var xh = new Xiaohua({name:'xiaohua',grade:2});
xh.run()
console.log(xh)
</script>
</head>
<body>
</body>
</html>
输出原型链的值是xiaohua,但是 原型链new Xiaohua =>xiaohua prototype =>Student prototype => object Prototype => null