JavaScript中的call和apply方法

prototype的工厂化写法

<script type="text/javascript">
Person.prototype.height = 1400;
Person.prototype.lang = 4900;
Person.prototype.carName = "BMW";
//上面的代码可以用下面的一整个prototype对象来简化
 Person.prototype = {
   height : 1400;
   lang : 4900;
   carName : "BMW";
}
</script>

 对象如何查看对象的构造函数——>constructor构造器

<script type="text/javscript">
function Person()
{
}
function Car()
{
}
var car = new Car();
</script>

控制台结果

>car.constructor
< function Car(){    //返回构造这个对象的构造函数,constructor存在
           }
>

car的constructor对象从何而来,看下面的控制台结果

>Car.prototype
< Object(){
   >constructor:Car() //这是系统自动生成的,在控制台表现为浅粉色,如果是自己在原型中添加的是深紫色
   >__proto__:Object    //这是系统自动生成的,在控制台表现为浅粉色,如果是自己在原型中添加的是深紫色
}

constructor的目的就是为了让所有的构造对象,在找它的构造函数的时候能找的到,也就是constructor就是对象的构造器属性,方便寻找它的构造函数

如果将Car()的构造器变为Person(),代码如下;

Car.prototype = {
constructor:Person
}

那么控制台结果如下

>car.constructor
< function Person(){
           }

可见,人为的修改构造函数的constructor使他不指向自己,转而指向另一个函数,那么由此函数构造的对象的constructor也会指向那个函数。

原始对象的toString方法

首先undefined不能调用toString,因为undefined没有原型,也就不存在tostring方法

var num =123;

num.toString();

>object

num是数字类型,但是它可以通过包装类来一层层的向上寻找,从而能够调用toString方法

var num = 123;
//num.toString(); --> new Number(num).toString();
Number.prototype.toString = function(){
//此函数是重写了object的toString方法
}
//Number.prototype.__proto__ = object.prototype

重写toString方法的有

//object.prototype.toString
//Number.prototype.toString
//Boolean.prototype.toString
//String.prototype.toString

JavaScript精读不准的问题

使用toFixed时精度不准,Math.floor是向下取整,Math.ceil是向上取整。

在控制台计算0.14*100 > 14.000000000000002,误差较小,精度精确到(可正常计算的范围)小数点前16位,后16位

<script text = "text/javascript">
for(var i = 0;i < 10;i ++){
var num =Math.floor( Math.random() * 100 );
console.log(num);
}
</script>

call/apply

作用:改变this指向。区别:后面传的参数形式不同

function Person(name,age){
//this == obj
this.name = name;
this.age = age;
}
var person = new Person('deng',100);
var obj = {}   //创建一个新对象
Person.call(obj,'cheng',300); //使得Person里面的this(本来this是指向Windows的)指向obj,然后 
                              // 将后面的参数传到obj对象中,实参'cheng'对应形参name,100对应形 
                              // 参age

实际上在执行一个函数test()的时候就是在执行test.call();上面的例子中,对象obj借用Person方法来实现自己的功能,在控制台输入obj回车,此时对象object里面已经存在{name:“cheng”,age:300}

function Person(name,age,sex){
this.name = name;
this.name = age;
this.sex = sex;
}
function Student(name,age,sex,tel,grade){
Person.call(this,name,age,sex);
this.tel = tel;
this.grade = grade;
}
var student = new Student('sunny',123,'male',139,2017);

Student方法里面的Person.call()通过改变Person函数中this的指向,使他指向Student本身,从而借用Person方法,实现自己的功能,Person.call(this,name,age,sex);其中的this就指代Student。

再举一个例子

function Wheel(wheelSize,style){
this.style = style;
this.wheelSize = wheelsize;
}
function Sit(Comfortable,sitColor){
this.Comfortable = Comfortable;
this.sitColor = sitColor;
}
function Model(height, width, len){
this.height = height;
this.width = width;
this.len =len;
}
function Car(wheelSize,style,Comfortable,sitColor,height, width, len){
Wheel.call(this,wheelSize,style);
Sit.call(this,Comfortable,sitColor);
Model.call(this,height, width, len);
}
var car = new Car(100,'花里胡哨','真皮','red','1800','1900','2000');

上面的Car()函数还可以这样写

function Car(wheelSize,style,Comfortable,height, width, len){
Wheel.apply(this,[wheelSize,style]);
Sit.apply(this,[Comfortable,sitColor]);
Model.apply(this,[height, width, len]);
}

call需要把实参按照形参的个数传进去,apply需要传一个arguments数组。

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值