对象的继承属性
1.原型和原型链知识点
1.1如图所示
1.2对象继承属性
function Person(name,age){
this.name = name;
this.age = age;
}
//Student和Teacher继承Person的属性
function Student(score){
this.score = score;
}
function Teacher(salary){
this.score = salary;
}
//先实例化Person构造一个和Student的原型对象(Student.prototype)的关系,
//实例化Student对象,Student对象可以通过_proto_调用Person的属性
Student.prototype = new Person("lixi",23)
var s1 = new Student(200);
console.log(s1);
1.3构造函数属性的继承
function Person(name,age){
this.name = name;
this.age = age;
}
//通过Call函数指明对象和参数
function Student(name,age,score){
Person.call(this,name,age);
this.score = score;
}
function Teacher(name,age,salary){
Person.call(this,name,age);
this.score = salary;
}
var s1 = new Student("lisi",29,290);
console.log(s1);
2. call、apply使用
2.1、call的使用:
参数分析:第一个参数为this指向的问题,其余参数为函数的实参。
function fn(a,b){
console.log(this);
console.log(a+b);
}
fn.call(this,3,4);
2.2、apply使用:
参数分析:第一个参数为this指向的问题,第二个参数为一个参数数组。
function fn(a,b){
console.log(this);
console.log(a+b);
}
fn.apply(this,[3,4]);
3.高阶函数
· 函数可以作为参数被传递
· 函数可以作为返回值输出
3.1通过数组Array中的Map方法来处理数组中的元素,Map方法中的参数可以是函数。
function pow(x) {
return x * x;
}
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
arr.map(pow); // [1, 4, 9, 16, 25, 36, 49, 64, 81]
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
arr.map(String); // ['1', '2', '3', '4', '5', '6', '7', '8', '9']
3.2 Reduce用法
数组中的元素求和
[x1, x2, x3, x4].reduce(f) = f(f(f(x1, x2), x3), x4)
//比方说对一个Array求和,就可以用reduce实现:
var arr = [1, 3, 5, 7, 9];
arr.reduce(function (x, y) {
return x + y;
}); // 25
3.3 filter使用教程
过滤函数中返回为false的函数
var arr = [1, 2, 4, 5, 6, 9, 10, 15];
var r = arr.filter(function (x) {
return x % 2 !== 0;
});
r; // [1, 5, 9, 15]
4.闭包
1.函数外部的成员可以读取函数内部的成员
2.让函数内部成员始终存活与内存中