草庐IT

this_call

全部标签

javascript - node.js,setTimeout回调方法和 "this"

我正在尝试使用NodeJS编写一个简单的轮询应用程序。我想编写一个EventEmitter,它对计时器执行操作并根据该周期性操作的结果发出事件。我首先创建自己的对象并从EventEmitter继承。我使用setInterval启动计时器,并指定在计时器结束后调用的方法。在计时器回调方法中,我想引用我创建的对象的变量,但this似乎没有引用该对象。如何在此方法中引用我的变量?这是我的代码:varutil=require('util'),events=require('events'),timers=require('timers'),redis=require('redis');//de

javascript - jquery.each() - "this"与 valueOfElement

在jQuery.each()中循环,我一直认为this等同于valueOfElement。有人可以解释一下区别吗?例子:$.each(object,function(i,val){$('body').append('valueOfElement:'+typeofval+'-'+'this:'+typeofthis+'');});结果:valueOfElement:string-this:objectvalueOfElement:boolean-this:objectvalueOfElement:object-this:objectFiddle 最佳答案

Javascript (ECMA-6) 类魔术方法 __call 像 PHP

这是我的用例getSomeFields(persons,fields){letpersonsWithSpecificFields=[];_.each(persons,(person)=>{letpersonSpecificFields={};_.each(fields,(field)=>{//hereimthinkingtomodifythefieldtomatchthemethodname//(ifsomethinglike__callasinphpisavailable)//e.g.fieldisfirst_nameandiwanttochangeittogetFirstNamep

javascript - 使用 Meteor.methods 和 Meteor.call

我有以下服务器代码:Meteor.startup(function(){Meteor.publish("AllMessages",function(){lists._ensureIndex({location:"2d"});returnlists.find();});});Meteor.methods({getListsWithinBounds:function(bounds){lists._ensureIndex({location:"2d"});returnlists.find({"location":{"$within":{"$box":[[bounds.bottomLeftLn

JavaScript: var {left, ...props} = this.props;

我正在阅读Facebook的固定数据表的源代码,我发现了thisvar{left,...props}=this.props;这是什么意思?这是一个新的语义吗?我很困惑o.O 最佳答案 这是一种特殊形式的解构赋值proposedforES7(并热切地在jsx工具和Babel中实现)。它创建了两个变量:left和props。left的值为this.props.left。props是一个对象,具有this.props的所有其他属性(不包括left)。如果你在没有解构的情况下编写它,它看起来像这样:varleft=this.props.le

javascript - 如何在 jquery 中保留 'this' 的上下文

我有这样的东西:varSomething=function(){this.render=function(){};$(window).resize(function(){this.render();});}问题是在匿名函数内部'this'引用了window对象。我知道我可以做类似的事情:varSomething=function(){this.render=function(){};vartempThis=this;$(window).resize(function(){tempThis.render();});}但是有更好的方法吗?这看起来不太优雅。 最佳

javascript - 传递 $(this) 作为参数?

$(document).ready(function(){functionGetDeals(){alert($(this).attr("id"));}$('.filterResult').live("click",function(event){GetDeals();});});我需要将什么作为参数传递到函数GetDeals()中,以便我可以使用$(this)进行操作?提前致谢! 最佳答案 您可以将该函数用作您的事件句柄:$('.filterResult').live("click",GetDeals);(请注意,您不使用()来调用

javascript - 在 Javascript 中使用 var 和 this 有什么区别?

这些有什么区别?vara=13;this.b=21;document.write(a);document.write(b); 最佳答案 对于全局代码(不属于任何函数的代码),它们几乎是等价的,都在最后创建全局对象的属性。区别在于a,它已经用var语句声明,VariableInstantiation进程将使用全局对象作为可变对象(1),并将该属性定义为不可删除,例如:vara=13;deletea;//falsetypeofa;//"number"然后,b因为全局代码中的this值,指向全局对象本身,也将是一个全局属性,但是这个可以删

javascript - KnockoutJS fromJS 不工作 TypeError : Cannot call method 'fromJS' of undefined

我使用knockoutJS,当我使用“fromJS”时出现以下错误TypeError:Cannotcallmethod'fromJS'ofundefined我的JavaScript代码$(document).ready(function(){varPersonModel=function(data){ko.mapping.fromJS(data,{},this);};vardata=$.getJSON("http://localhost:40913/candidate/index/1",function(data){viewModel=newPersonModel(data);ko.a

javascript - d3.select(this) 适用于鼠标悬停,但不适用于鼠标悬停时调用的函数

我是javascript的新手,目前正在努力选择this对象,同时尝试进行d3选择。我制作了以下示例,其中包含我正在调用的函数和一个onmousemove事件:functionchangeFont(){d3.select(this).attr('font-size','2em')}....on('mousemove',function(){varmouse=d3.mouse(this);varxVal=mouse[0];//thiswouldwork,butnotwhenitscalledinafunction//d3.select(this)//.attr('font-size','