call和apply方法
/*
* @ 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方法的更多相关文章
- JS中 call() 与apply 方法
1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call ...
- JavaScript学习笔记(1))——————call,apply方法
学习前端也有一段时间了,但是效果甚微.利用时间不够充分,虽然是利用工作之余来学习.但是这不能成为我的借口. 今天学习了(其实看了很多遍)call apply方法. function abc(a,b){ ...
- angularjs $scope.$apply 方法详解
myApp.controller('firstController',function($scope,$interval){ $scope.date = new Date(); setInterval ...
- 《ES6基础教程》之 Call 方法和 Apply 方法
<script type="text/javascript"> // Call方法: // 语法:call(thisObj[,arg1,arg2,...,argN]) ...
- Js apply方法详解
我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里我做如下笔记,希望和大家 ...
- JS中的call()和apply()方法
1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call ...
- 优雅的数组降维——Javascript中apply方法的妙用
将多维数组(尤其是二维数组)转化为一维数组是业务开发中的常用逻辑,除了使用朴素的循环转换以外,我们还可以利用Javascript的语言特性实现更为简洁优雅的转换.本文将从朴素的循环转换开始,逐一介绍三 ...
- js巧用apply方法实现数组最值以及合并
尽管js的apply方法在平常的使用中并不多见,但是在某些地方使用的还是很有帮助性的,这里就和大家说两个比较实用的例子:1.数组最大最小值 求数组中的最大最小值,js有相应的方法:Math.min() ...
- 原生JS中apply()方法的一个值得注意的用法
今天在学习vue.js的render时,遇到需要重复构造多个同类型对象的问题,在这里发现原生JS中apply()方法的一个特殊的用法: var ary = Array.apply(null, { &q ...
- scala 学习笔记(04) OOP(上)主从构造器/私有属性/伴生对象(单例静态类)/apply方法/嵌套类
一.主从构造器 java中构造函数没有主.从之分,只有构造器重载,但在scala中,每个类都有一个主构造器,在定义class时,如果啥也没写,默认有一个xxx()的主构造器 class Person ...
随机推荐
- C++实现 找出10000以内的完数
C++实现 找出10000以内的完数 #include <stdio.h> int main(){ int n; // 用户输入的整数 int i; // 循环标志 printf(&quo ...
- springMVC下的javascript调试
最近想弄一个hadoop的管理界面,所以在网上下了一个名为jeecg的快速开发平台,由于工作之后没有用过java做网站,遇到了好多小问题,其中一个就是现在要说的javascript脚本调试的问题.说来 ...
- 【DataStructure】Linked Data Structures
Arrayss work well for unordered sequences, and even for ordered squences if they don't change much. ...
- pick王菊?作为“菊外人”的程序员能做点什么?
转载:https://mp.weixin.qq.com/s/s1cb9Ij6ouTYYCZovLhXTA 最近,想必大家的朋友圈都被“王菊”占领了,打开朋友圈到处可以见到“pick王菊”.“陶渊明”. ...
- [mysql] 随机查询 效率比较
select primary_count as primaryCount, primary_score as primaryScore, junior_count as juniorCount, ju ...
- Windoows窗口程序三
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h& ...
- 安装cx_Oracle 遇到的杂项问题
1. 解决方法: 将xc用户添加进sudousers 2.安装VMware Tools 更新 http://pubs.vmware.com/vsphere-50/index.jsp?topic=%2F ...
- u3d发布成全屏的方式
using UnityEngine; using System.Collections; public class example : MonoBehaviour { public voi ...
- Oracle过程及函数的参数模式详解
一.In.out.in out模式 在Oracle中过程与函数都可以有参数,参数的类型可以指定为in.out.in out三种模式. 三种参数的具体说明,如下图所示: (1)in模式 in模式是引用传 ...
- 数据挖掘Apriori算法——学习笔记
关联规则.频繁项集.支持度.置信度 关联规则挖掘: 一起购买的商品 支持度(support) 支持度会随着物品增多而减小.因为是同时购买的比率. 置信度(Confidence) 频繁且强规则,有一定意 ...