apply

[转] JS中的call()方法和apply()方法用法总结

孤街浪徒 提交于 2020-03-07 00:11:56
//例1 <script> window.color = 'red'; document.color = 'yellow'; var s1 = {color: 'blue' }; function changeColor(){ console.log(this.color); } changeColor.call(); //red (默认传递参数) changeColor.call(window); //red changeColor.call(document); //yellow changeColor.call(this); //red changeColor.call(s1); //blue </script> //例2 var Pet = { words : '...', speak : function (say) { console.log(say + ''+ this.words) } } Pet.speak('Speak'); // 结果:Speak... var Dog = { words:'Wang' } //将this的指向改变成了Dog Pet.speak.call(Dog, 'Speak'); //结果: SpeakWang //例1 <script> window.number = 'one'; document.number = 'two'; var

js中call,apply,bind的实现原理()

一世执手 提交于 2020-03-04 02:41:08
/* author:yangJian */ // js中call,apply,bind的实现原理() // 三者的区别,都是改变this指针,call和apply主要是参数区别,bind返回的是一个函数体,而call和apply是立即执行 // call的实现 function fn1 (str1,str2,str3){ console.log(this,str1,str2,str3); } function fn2(){ } Function.prototype.call = function (context){ // 避免传入的是基本类型,使用object进行对象化 context = context ? Object(context) : window; context.fn = this; let args = []; for (let i = 1;i 来源: https://www.cnblogs.com/chengxuxing/p/12406192.html

Extract values from a correlation matrix according to their p-value in a second matrix

淺唱寂寞╮ 提交于 2020-03-03 10:14:44
问题 I have created a correlation matrix with an external program (SparCC). I have calculated p-values from the same data in SparCC as well and I end up with two objects which I imported into R, let's call them corr and pval and > ncol(corr)==nrow(corr) [1] TRUE > ncol(pval)==nrow(pval) [1] TRUE and > colnames(corr)==rownames(pval) [1] TRUE ... and the same the other way around. Since the matrices (or should I be using data.frame ?) are fairly large (about 1000 items), I would like to extract the

Extract values from a correlation matrix according to their p-value in a second matrix

牧云@^-^@ 提交于 2020-03-03 10:14:09
问题 I have created a correlation matrix with an external program (SparCC). I have calculated p-values from the same data in SparCC as well and I end up with two objects which I imported into R, let's call them corr and pval and > ncol(corr)==nrow(corr) [1] TRUE > ncol(pval)==nrow(pval) [1] TRUE and > colnames(corr)==rownames(pval) [1] TRUE ... and the same the other way around. Since the matrices (or should I be using data.frame ?) are fairly large (about 1000 items), I would like to extract the

第8节_调用函数

最后都变了- 提交于 2020-03-03 05:10:08
def age ( a ) : return 18 <= a < 30 def level ( s ) : return 85 <= s <= 100 定义函数,age,给age赋值a,a的范围见上 定义函数,level,给level赋值s,s的范围见上 stu = stu . loc [ stu [ 'Age' ] . apply ( age ) ] Stu.[‘Age’]这一列用age函数, Loc,保留此列 stu = stu . loc [ stu [ 'Age' ] . apply ( age ) ] . loc [ stu [ 'Score' ] . apply ( level ) ] 同上, Stu.[‘Age’]这一列用age函数, Stu.[‘Score’]这一列用level函数, Loc,保留此列 以下是代码优化: stu = stu . loc [ stu . Age . apply ( age ) ] . loc [ stu . Score . apply ( level ) ] stu[‘Age’]可优化为stu.Age stu = stu . loc [ stu . Age . apply ( lambda a : 18 <= a < 30 ) ] 定义函数部分 def age(a): return 18<=a<30 可优化为lambda表达式

js apply,call,arguments,callee,caller详解

↘锁芯ラ 提交于 2020-03-02 08:35:26
apply与 call 主要解决一下几个问题: 1.apply和call的区别在哪里 2.什么情况下用apply,什么情况下用call 3.apply的其他巧妙用法(一般在什么情况下可以使用apply) apply:方法能劫持另外一个对象的方法,继承另外一个对象的属性. Function.apply(obj,args)方法能接收两个参数 obj:这个对象将代替Function类里this对象 args:这个是数组,它将作为参数传给Function(args-->arguments) call:和apply的意思一样,只不过是参数列表不一样. Function.call(obj,[param1[,param2[,…[,paramN]]]]) obj:这个对象将代替Function类里this对象 params:这个是一个参数列表 1.apply示例: <script type="text/javascript"> /*定义一个人类*/ function Person(name,age) { this.name=name; this.age=age; } /*定义一个学生类*/ functionStudent(name,age,grade) { Person.apply(this,arguments); this.grade=grade; } //创建一个学生类 var student

num02---策略模式

帅比萌擦擦* 提交于 2020-03-01 13:12:05
只听名字总没有什么头绪,什么策略? 情景: 超市活动,有满减,有打折,有正常收费,求 不同情况下的付款额。 ①创建 付款基类抽象类,有一个抽象方法apply用于计算活动后实际付款额 //付款 基类 public abstract class CashSuper {   //计算付款额   public abstract double apply(double money); } ② 创建正常付费、打折付费、满减付费 三种付费方式类,分别继承付费基类。各自重写自己的apply方法。 // 满A返现(B) 方式 public class CashReturn extends CashSuper {   private double A;   private double B;   public CashReturn(String a, String b) {     super();     A = Double.valueOf(a.toString());     B = Double.valueOf(b.toString());   }   @Override   public double apply(double money) {     if(money >= A){       return money - B;     }else{       return money;

理解Scala的apply

戏子无情 提交于 2020-03-01 13:02:42
理解Scala的apply Mathematicians(数学家) have their own little funny ways, so instead of saying "then we call function f passing it x as a parameter" as we programmers would say, they talk about "applying function f to its argument x". In mathematics and computer science, Apply is a function that applies functions to arguments. https://en.wikipedia.org/wiki/Apply Apply serves the purpose of closing the gap between Object-Oriented and Functional paradigms(范式) in Scala. Every function in Scala can be represented as an object. Every function also has an OO type: for instance, a function that takes an

JavaScript设计模式之二:this、call和apply

早过忘川 提交于 2020-03-01 10:28:52
(本节内容摘自:Javascript设计模式与开发实践一书,作为自己的笔记保存,希望对有需要的朋友有用) this、call和apply在Javascript编程中应用非常广泛,所以,我们必须先了解它们。 一、this Javascript中的this总是指向一个对象,而它又是基于执行环境动态绑定,以下有4中情况可以用来分析。 当函数作为对象的方法时,this指向该对象,看下面的代码: var obj = { a: 1, getA: function(){ alert(this === obj); //true alert(this.a); //1 } }; obj.getA(); 当作为普通函数调用时,此时的this总是指向全局对象window window.name = 'globalName'; var getName = function(){ return this.name; }; console.log(getName()); //globalName 下面来一个实际的例子,我们在一个div节点内部,定义一个局部的callback方法,当这个方法被当做普通函数调用时,callback内部的this就指向了window,如下代码: <html> <body> <div id="div1">我是一个Div</div> </body> <script> window.id

New操作内部步骤、call/apply/bind区别、alert种类、逗号语句

孤者浪人 提交于 2020-03-01 10:02:43
一、new操作符具体干了什么? var Person = Function(name){ this.name = name } var p = new Person; // new操作符做了以下三件事 var p = {}; // 创建了一个空对象 p.__proto__ = Person.prototype; // 将这个对象的 __proto__成员指向了Person.prototype Person.call(p); // 将Person函数的this指针指向换成p,然后在调用Person函数 二、call 、apply和bind的区别 (一)、相似之处: 1、都是用来改变函数的this对象的指向的。 2、第一个参数都是this要指向的对象。 3、都可以利用后续参数传参。 (二)、区别: 片段一 var xw = { name:'小王', gender:'男', aage:24, say: function(){ alert(this.name + " , " + this.gender + " ,今年" + this.age) } } var xz = { name:'小张', gender: '男', age:25 } xw.say() 片段一,显示为:小王,男,今年24岁。 那么如何用wx的say方法来显示xz的数据呢? 1、call的用法:xw.say.call(xh