object.prototype.call

/*
* object.prototype.call
* @ 当一个object没有某个方法,但是其他的有,我们可以借助call或apply用其它对象的方法来操作。
* @ 语法: fun.call(thisArg[, arg1[, arg2[, ...]]])
* @ param: thisArg {object} //当前引用对象
* @ 不传参数,传null,undefined, this指向window对象
* @ 传递另一个函数的函数名fun2, this指向函数fun2的引用
* @ 传递一个对象,函数中的this指向这个对象
* @ 值为原始值(数字,字符串,布尔值), this会指向该原始值的自动包装对象,如String,Number,Boolean
* @ param: arg1, arg2, ... {object} // arguments参数
*/

call函数中的this指向

function a(){
console.log(this);
}
function b(){} var objthis = {name: "Alan"}; //定义对象
a.call(); // window
a.call(null); // window
a.call(undefined); // window
a.call(1); // Number {[[PrimitiveValue]]: 1}
a.call(""); // String {length: 0, [[PrimitiveValue]]: ""}
a.call(true); // Boolean {[[PrimitiveValue]]: true}
a.call(b); // function b(){}
a.call(objthis); // Object {name: "Alan"}

使用call对象的构造函数链

function Product(name, price){
this.name = name;
this.price = price; if(price < 0){
throw RangeError('Cannot create product ' + this.name + ' with a negative price');
}
} // call方法
function Food(name,price){
Product.call(this,name,price);
this.category = "food";
} // 等同于
function Food(name,price){
this.name = name;
this.price = price; if(price < 0){
throw RangeError('Cannot create product ' + this.name + ' with a negative price');
} this.category = "food";
}

使用call调用匿名函数

var animals = [
{
species: "Lion",
name: "king"
}, {
species: "Whale",
name: "Fail"
}
] for(var i = 0; i < animals.length; i++){
(function(i){
this.print = function(){
console.log("#" + i + " " + this.species + ": " + this.name);
} this.print();
}).call(animals[i],i); // 等同于
/*(function(){
this.print = function(){
console.log("#" + i + " " + animals[i].species + ": " + animals[i].name);
}
this.print();
})();*/
}

使用call调用函数的上下文this

function greet(){
var reply = [this.person, "Is An Awesome", this.role].join(" ");
console.log(reply);
} var obj = {
person: "Douglas Crockford", role: "Javascript Developer"
}; greet.call(obj);

以DOM为例子

function changeStyle(attr, value){
this.style[attr] = value;
}
var box = document.getElementById('box');
window.changeStyle.call(box, "height", "200px");
window.changeStyle.apply(box, ['height', '200px']);

// 不用call

function say(name){
console.log(this + "," + name);
}
say.call2 = function( thisObj, arg1 ) {
thisObj = new Object( thisObj );
thisObj.say = this;
return thisObj.say(arg1);
}; say.call2("hola","Mike");

object.prototype.call的更多相关文章

  1. Object.prototype和Function.prototype一些常用方法

    Object.prototype 方法: hasOwnProperty 概念:用来判断一个对象中的某一个属性是否是自己提供的(主要是判断属性是原型继承还是自己提供的) 语法:对象.hasOwnProp ...

  2. 利用Object.prototype.toString方法,实现比typeof更准确的type校验

    Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...

  3. Object.prototype 与 Function.prototype 与 instanceof 运算符

    方法: hasOwnProperty isPrototypeOf propertyIsEnumerable hasOwnProperty 该方法用来判断一个对象中的某一个属性是否是自己提供的( 住要用 ...

  4. instanceof, typeof, & Object.prototype.toString

    /** * * @authors Your Name (you@example.org) * @date 2016-11-18 09:31:23 * @version $Id$ */instanceo ...

  5. 判断一个变量的类型Object.prototype.toString.call

    var num = 1;alert(Object.prototype.toString.call(num)); // [object Number]var str = 'hudidit.com';al ...

  6. Object.prototype.toString.call() 区分对象类型

    判断一个对象的类型: /** * 判断对象是否为数组 * @param {Object} source 待判断的对象 * @return {Boolean} true|false */ Object. ...

  7. Object.prototype.toString.call()进行类型判断

    为什么类型判断用到Object.prototype.toString.call()进行类型判断,而不用typeof()呢? 然后翻了一下资料: Typeof 在使用 ]));/));));//[obj ...

  8. toStirng()与Object.prototype.toString.call()方法浅谈

    一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种类型转化为字符串类型的呢? 通过下面几个例子,我们便能获得答案: 1.将boolean类型的值转 ...

  9. [Effective JavaScript 笔记]第47条:绝不要在Object.prototype中增加可枚举的属性

    之前的几条都不断地重复着for...in循环,它便利好用,但又容易被原型污染.for...in循环最常见的用法是枚举字典中的元素.这里就是从侧面提出不要在共享的Object.prototype中增加可 ...

  10. JavaScript中toStirng()与Object.prototype.toString.call()方法浅谈

    toStirng()与Object.prototype.toString.call()方法浅谈 一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种 ...

随机推荐

  1. 对设计领域中Tile和Card的理解

    前端工程师离不开设计, 谈到设计就要想到大名鼎鼎的material design主题, 而material是以card为经典单元的, card即卡片, 是层次化模型的最小模块, 用于提供扁平化的信息, ...

  2. mysql数据库对时间进行默认的设置

    //----------------------------------------------------------sql语句----------------------------------- ...

  3. javaScript之function定义

    背景知识 函数定义 在javaScript中,function的定义有3种: 1.匿名定义                function(){} 2.非匿名定义                fun ...

  4. C语言 字面量

    在计算机科学中,字面量(literal)是用于表达源代码中一个固定值的表示法(notation). 几乎所有计算机编程语言都具有对基本值的字面量表示,诸如:整数.浮点数以及字符串: 而有很多也对布尔类 ...

  5. Mac之安装zsh

    1.安装homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/in ...

  6. (转)RGB接口和i80接口的区别

     在嵌入式的主流 LCD屏中主要支持两大类的硬件接口,一种是常见的RGB接口,另外一种是MCU接口.后面因为最早是针对单片机的领域在使用,因此得名.后在中低端手机大量使用,其主要特点是价格便宜的. M ...

  7. 【Java面试题】24 sleep() 和 wait() 有什么区别? 详细解析!!!!

    第一种解释: 功能差不多,都用来进行线程控制,他们最大本质的区别是:sleep()不释放同步锁,wait()释放同步缩.         还有用法的上的不同是:sleep(milliseconds)可 ...

  8. Nginx的启动与停止,重启

    1.先确定nginx所在的文件位置 如: 重启 1.验证nginx配置文件是否正确 方法一:进入nginx安装目录sbin下,输入命令./nginx -t 2.重启Nginx服务 方法一:进入ngin ...

  9. LLE局部线性嵌入算法

    非线性降维 流形学习 算法思想有些类似于NLM,但是是进行的降维操作. [转载自] 局部线性嵌入(LLE)原理总结 - yukgwy60648的博客 - CSDN博客 https://blog.csd ...

  10. Phpcms V9当前栏目及所有二级栏目下内容调用标签

    在二级栏目列表页调用: <!--* 获取子栏目* @param $parentid 父级id* @param $type 栏目类型* @param $self 是否包含本身 0为不包含* @pa ...