一、ECMScript简单介绍
ECMAScript(European Computer Manufacturers Association)是通过ECMA-262标准化的脚本程序设计语言,通常称为Javascript/JScript,所以可理解为是JavaScript的一个标准,但实际上后两者是ECMA-262标准的实现和扩展。
二、apply()方法
apply方法是应用某一对象的一个方法,用另一个对象替换当前对象;
语法 apply(thisObject,Array) 两个参数
参数 thisObject 可选项 将被用作当前对象的对象;
Array 可选项 将被传递给该函数的参数数组;
说明;若Array不是一个有效的数组或者不是arguments对象,将导致一个TypeError;
若未提供Array和thisObject任一个参数,那么Global对象将被用作thisObject,并且无法传递任何参数。
function.apply(thisObject,Array)//thisObject这个对象将代替function类里this对象;
示例:构造继承中的应用 可以实现多继承
//定义一个动物类 父类
function anminl(name,type) {
this.name=name;
this.type=type;
}
//定义一个猫科类 子类
function Cat(name,type,age) {
anminl.apply(this,arguments);
//arguments 指一个数组即["tom","bosimiao","12"]
this.age=age;
}
//创建一个猫类
var cat=new Cat("tom","bosimiao","12");//实例化
console.log(cat.name);
console.log(cat.type);
console.log(cat.age);
arguments属性:为当前执行的function对象返回一个arguments对象;
function.arguments function参数是当前执行函数的名称,可省略;
作用:通过arguments属性,函数可以处理可变数量的参数,arguments对象的length属性包含了传递给函数的参数的数目。对于arguments对象所包含的单个参数,其访问方法与数组中所包含的参数的访问方法相同。
快速取出数组中的最大值、最小值除了冒泡,数组快排,还可以使用此方法Math.max.apply Math.min.apply
var arr=[1,3,4,5,2,6,7];
console.log(Math.max.apply(null,arr));
console.log(Math.min.apply(null,arr));
//调用Math方法的比较大小方法 max()返回大的值 min()返回小的值
实现合并数组 Array.prototype.push.apply
var arr=[1,3,4,5,2,6,7];
var arr1=[1,4,5,6,7,8];
Array.prototype.push.apply(arr,arr1);//给arr内追加的内容是arr1的值
console.log(arr);
二、call()方法
call和apply用法一致,只是其参数列表不同,换句话说就是在使用call()方法时,传递给函数的参数必须逐个例举出来。
语法:function.call(obj,[参数1[,参数2[,参数3....[,参数n]]]])
obj:指这个对象将代替function类里的this对象;
参数:指一个参数列表
例如:
//定义一个动物类 父类
function anminl(name,type) {
this.name=name;
this.type=type;
}
//定义一个猫科类 子类
function Cat(name,type,age) {
anminl.call(this,name,type);//不同之处 call需要把父类参数一一列举
this.age=age;
}
//创建一个猫类
var cat=new Cat("tom","bosimiao",12,"fish");//实例化
console.log(cat.name);
console.log(cat.type);
console.log(cat.age);
console.log(cat instanceof Cat);//true
console.log(cat instanceof anminl);//false