/*
* @ 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. Maven_POM配置详解

    本文转载,方便以后查阅,转载地址:http://blog.csdn.net/ithomer/article/details/9332071 <project xmlns="http:/ ...

  2. 时间控件 BeatPicker

    项目展示 样式异样,可修改此样式,详见官网:https://github.com/ACT1GMR/BeatPicker --- 开始使用 1.引入js&css文件 <link rel=& ...

  3. Spring Boot 日志记录 SLF4J

    Spring Boot 日志记录 SLF4J 2016年01月12日 09:25:28 阅读数:54086 在开发中打印内容,使用 System.out.println() 和 Log4j 应当是人人 ...

  4. 一个残酷的生鲜O2O之梦

    三个年轻人,毕业一年,上海浦东张江开了一家生态有机蔬菜店-稻香麦甜. 首先,这不是一个关于成功励志的故事,相反的,我们走向了悬崖.经营2个月,最终以签字转让结束了实体店. 准备阶段 我,首先辞职,那时 ...

  5. 30个实用的jQuery选项卡/导航教程推荐

    很多网站设计中都使用了选项卡(tabs),在制作选项卡时应用jQuery能够实现很多炫酷的过渡和动画效果.本文为你介绍30个实用的jQuery选项卡教程,希望对你有帮助. 1. Animated Ta ...

  6. thinkphp 点击分类显示分类下的文章(完整)

    控制器 <?php // 本类由系统自动生成,仅供测试用途 class IndexAction extends Action { public function index(){ $cate=M ...

  7. selenium测试(Java)-- 验证信息(八)

    package com.test.validationinfor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.f ...

  8. python中的矩阵、多维数组----numpy

    https://docs.scipy.org/doc/numpy-dev/user/quickstart.html  (numpy官网一些教程) numpy教程:数组创建 python中的矩阵.多维数 ...

  9. Floyd算法思想

    关键词:代数.图论.矩阵.松弛技术.动态规划 Floyd算法是一个经典的动态规划算法.用通俗的语言来描述的话,首先我们的目标是寻找从点i到点j的最短路径.从动态规划的角度看问题,我们需要为这个目标重新 ...

  10. CentOS后台运行和关闭、查看后台任务命令

    fg.bg.jobs.&.nohup.ctrl+z.ctrl+c 命令 一.& 加在一个命令的最后,可以把这个命令放到后台执行,如 watch -n 10 sh test.sh &am ...