草庐IT

this_and_that

全部标签

javascript - 使用 this/self 引用的 javascript 闭包会导致内存泄漏吗?

根据我对内存泄漏的理解,在闭包中引用范围外的var会导致内存泄漏。但创建“that”var以保留“this”引用并在闭包中使用它也是一种常见的做法,尤其是对于事件。那么,做这样的事情有什么用:SomeObject.prototype.createImage=function(){varthat=this,someImage=newImage();someImage.src='someImage.png';someImage.onload=function(){that.callbackImage(this);}};这不会给项目增加一点漏洞吗? 最佳答案

javascript - Codility 训练 : Find the maximal number of clocks with hands that look identical when rotated

这是问题的链接:https://codility.com/demo/take-sample-test/clocks问题是我不能从中得到100分(只有42分)。运行时间还可以,但对于某些测试用例,代码给出了错误的答案,但我无法弄清楚问题出在哪里。有人可以帮帮我吗?这是我的代码:functionrotate(arr){varmin=arr.reduce(function(a,b){returna>b?b:a});while(arr[0]!=min){varfirst=arr.shift();arr.push(first);}}functionsolution(A,P){varpositio

javascript - Jade : load external javascript and call function

我正在学习Express/Node/Jade,现在我想在Jade文件中包含一个来自公共(public)文件夹的javascript文件,只用于该页面。例如,在jade文件中我输入:script(src='/javascripts/test.js')在test.js里面我有一个函数functioncheck_test(){return"It'sworking!"}然后我尝试通过以下方式调用Jade中的函数-vartest_response=check_test()比我得到的错误说“undefinedisnotafunction”和test.js根本没有加载。显然Jade不会加载文件,它们

javascript - 错误 : Task x can't support dependencies that is not an array of strings

我正在关注thistutorial如何开始使用gulp和browserify(以及其他插件)。结构如下:.├──gulpfile.js└──gulp  ├──index.js  └──tasks  ├──browserify.js  └──minifyCss.js/*gulpfile.js*/vargulp=require('./gulp')(['minifyCss','browserify']);gulp.task('default',['minifyCss','browserify']);/*index.js*/vargulp=require('gulp');module.expo

javascript - Foundation 6 - 控制台警告 : Tried to initialize magellan (any JS plugin) on an element that already has a Foundation plugin

我使用bower安装了Foundation6。每次我使用任何Foundation6-JavaScriptbasedplugin时,我都会在控制台中收到多个warning。确切的警告:TriedtoinitializemagellanonanelementthatalreadyhasaFoundationplugin.我的脚本包括如下所示:$(document).foundation();该警告由foundation.js中180行的以下代码触发://Foreachpluginfound,initializeit$elem.each(function(){var$el=$(this),o

javascript - Node.js 事件发射器 : How to bind a class context to the event listener and then remove this listener

有没有办法在事件监听器方法中访问类上下文并有可能删除监听器?示例1:import{EventEmitter}from"events";exportdefaultclassEventsExample1{privateemitter:EventEmitter;constructor(privatetext:string){this.emitter=newEventEmitter();this.emitter.addListener("test",this.handleTestEvent);this.emitter.emit("test");}publicdispose(){this.emi

javascript - 使用 express nodejs 获取此错误 auth/operation-not-supported-in-this-environment

我在我的项目中使用Firebase,但在使用google凭据登录时出现此错误auth/operation-not-supported-in-this-environment。.hbs文件代码脚本代码functionloginWithGoogle(event){$.ajax({url:"/session/google/login",type:"POST"}).done(function(data){error=JSON.stringify(data);console.log(error);M.toast({html:error})});}express代码router.post('/se

javascript - 为什么需要匿名函数来使用 setTimeout 保留 "this"

我已经多次使用setTimeout传递函数作为引用,例如setTimeout(someFunction,3000);在某些情况下,为了保留this的值,我不得不事先将其分配给一个变量,但不明白为什么以下内容不起作用:varlogger={log:function(){varthat=this;console.log(that.msg);setTimeout(that.log,3000);},msg:"test"};logger.log();然而,使用匿名函数确实有效:varlogger={log:function(){varthat=this;console.log(that.msg)

javascript - AngularJS + Grunt + Protractor : keepAlive and watch

我目前正在使用E2E测试我的AngularJS应用程序和Grunt中的Protractor。我按照https://www.npmjs.org/package/grunt-protractor-runner安装,我有2个问题:首先:我想知道,如果有可能,我使用Karma进行的单元测试会产生这种行为:启动测试服务器运行所有测试当测试文件或任何其他html/js文件更改时,重新运行所有测试我非常喜欢这种实时监视和自动重新加载的方式,因为它减少了整个套件的启动时间,并使一切变得更快。但是对于Protractor,我有一个问题:每次我用Grunt开始测试时,测试都会执行,然后一切都会关闭。虽然,

javascript - React.JS this.state 未定义

我目前在React.JS中有这个组件,它在数组中显示传递给它的所有图像,onMouseOver它在下面显示一个按钮。我计划使用setState检查变量hover是真还是假,并相应地切换该图像的按钮,但是我不断收到以下错误:UncaughtTypeError:Cannotreadproperty'state'ofundefinedvarImageList=React.createClass({getInitialState:function(){returnthis.state={hover:false};},getComponent:function(index){console.lo