大前端关于JS高级中对象继承属性和构造函数继承属性

对象的继承属性

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.让函数内部成员始终存活与内存中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值