/*
* @ call和apply方法
* @ 当一个object没有某个方法,但是其他的有,我们可以借助call或apply用其它对象的方法来操作。
* @ (有方法的)对象.call("环境的上下文本对象",参数)
* @ 通过call和apply,我们可以实现对象继承。示例
*/
/*function product(test){
alert(test);
} function log(){ }
product.call(log,2);*/ /*function product(test){
alert(test);
} function log(){
product.call(this,2);
}
log();*/ 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";
} // 以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']);*/ /*// 实现继承
var Parent = function(){
this.name = "yc",
this.age = "1"
} var child = {};
Parent.call(child); // 实现继承*/ /*log("%c红色的字, %c绿色的字, %c大象", "color:red", "color:green", "line-height:100px;padding:50px;background:url(http://fanjs.net/res/img/favicon.ico");*/ 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
* @ 当一个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 negative price");
}
} function Food(name,price){
Product.call(this,name,price);
this.category = "food"
}
var cheese = new Food("feta",5); // 使用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);

  

call和apply方法的更多相关文章

  1. JS中 call() 与apply 方法

    1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[,   [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call ...

  2. JavaScript学习笔记(1))——————call,apply方法

    学习前端也有一段时间了,但是效果甚微.利用时间不够充分,虽然是利用工作之余来学习.但是这不能成为我的借口. 今天学习了(其实看了很多遍)call apply方法. function abc(a,b){ ...

  3. angularjs $scope.$apply 方法详解

    myApp.controller('firstController',function($scope,$interval){ $scope.date = new Date(); setInterval ...

  4. 《ES6基础教程》之 Call 方法和 Apply 方法

    <script type="text/javascript"> // Call方法: // 语法:call(thisObj[,arg1,arg2,...,argN]) ...

  5. Js apply方法详解

    我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里我做如下笔记,希望和大家 ...

  6. JS中的call()和apply()方法

    1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[,   [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call ...

  7. 优雅的数组降维——Javascript中apply方法的妙用

    将多维数组(尤其是二维数组)转化为一维数组是业务开发中的常用逻辑,除了使用朴素的循环转换以外,我们还可以利用Javascript的语言特性实现更为简洁优雅的转换.本文将从朴素的循环转换开始,逐一介绍三 ...

  8. js巧用apply方法实现数组最值以及合并

    尽管js的apply方法在平常的使用中并不多见,但是在某些地方使用的还是很有帮助性的,这里就和大家说两个比较实用的例子:1.数组最大最小值 求数组中的最大最小值,js有相应的方法:Math.min() ...

  9. 原生JS中apply()方法的一个值得注意的用法

    今天在学习vue.js的render时,遇到需要重复构造多个同类型对象的问题,在这里发现原生JS中apply()方法的一个特殊的用法: var ary = Array.apply(null, { &q ...

  10. scala 学习笔记(04) OOP(上)主从构造器/私有属性/伴生对象(单例静态类)/apply方法/嵌套类

    一.主从构造器 java中构造函数没有主.从之分,只有构造器重载,但在scala中,每个类都有一个主构造器,在定义class时,如果啥也没写,默认有一个xxx()的主构造器 class Person ...

随机推荐

  1. 关于document.createDocumentFragment()(转)

    documentFragment 是一个无父对象的document对象. 他支持以下DOM2方法: appendChild, cloneNode, hasAttributes, hasChildNod ...

  2. centos 6.5配置samba

    Samba简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件,而SMB是Server Message Block的缩写,即为服务器消息块 ,SMB主要是作为Microso ...

  3. 教你如何架设linux邮件服务器postfix

    检查linux是否有安装postfix和dovecot 检查命令如下: Rpm  -qa |grep  postfix; Rpm –qa |grep dovecot; 如果没有显示任何数据,表明没有安 ...

  4. 【Java面试题】7 构造器Constructor是否可被override?

    构造器Constructor不能被继承,因此不能重写Override,但可以被重载Overload. Constructor不能被继承,所以Constructor也就不能被override.每一个类必 ...

  5. 【Deep learning】NLP

    http://www.tuicool.com/articles/EvaQJnJ http://cs224d.stanford.edu/syllabus.html

  6. ThinkPHP Mongo驱动update方法支持upsert参数

    Mongo数据库update操作有一个相对于Mysql的关键特性,它可以使用upsert模式,当更新的数据不存在时,直接插入,但是ThinkPHP的Mongo驱动居然不支持这一特性,没办法,自力更生了 ...

  7. 如何用MathType快速输入公式

    我们在写论文的时候,如果涉及到数学公式的话,我们一般都会使用MathType软件,但同时对于一些新手来说不太用使用此款软件,输入公式时就会很慢,今天教大家如何利用MathType快速输入公式. 具体方 ...

  8. 换个思路理解Javascript中的this

    https://segmentfault.com/a/1190000010328752

  9. Ext3.4--TreeGridDemo

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CategoryProper ...

  10. linux命令在文件中根据命令查找

    find . -type f -name "*.tmp" | xargs grep -ri "2016-08-30 04:00:00|2016-08-30 05:00:0 ...