草庐IT

in_this_iter

全部标签

javascript - for( … in …) 不适用于数组

我有点困惑:我有一个这样的命令列表:varcommands=[{"command":"read"},{"command":"write"},{"command":"login"}];如果我尝试访问这样的命令之一,它会起作用:console.log(commands[0]["command"]);//Outputis"read"console.log(commands[0].command);//Outputis"read"但如果我尝试这样做,输出总是未定义的:for(commandincommands)console.log(command["command"]);//undefine

javascript - 从 javascript 回调中引用 "this"

我在Javascript中进行面向对象编码的方式一直困扰着我。当有回调时,我经常想引用最初调用该函数的对象,这导致我做这样的事情:MyClass.prototype.doSomething=function(obj,callback){varme=this;//ughobj.loadSomething(function(err,result){me.data=result;//ughcallback(null,me);});}首先,创建附加变量似乎总是……对我来说太过分了。此外,我想知道它是否会通过将“me”变量传回回调而最终导致问题(循环引用?非GCd对象?)。有没有更好的方法来解决

javascript - javascript 中的 for..in 和 for each..in 有什么区别?

javascript中的for..in和foreach..in语句有什么区别?是否存在我不知道的细微差别,或者它们是否相同并且每个浏览器都有不同的名称? 最佳答案 “foreach...in”对指定对象属性的所有值迭代指定变量。例子:varsum=0;varobj={prop1:5,prop2:13,prop3:8};foreach(variteminobj){sum+=item;}print(sum);//prints"26",whichis5+13+8Source“for...in”以任意顺序在对象的所有属性上迭代指定变量。例子

javascript - 单击表单中的 div 元素后,this.form.submit() 不起作用

我尝试使用鼠标点击来测试表单提交,但表单似乎无法使用vanillajavascript提交。我正在使用这个简单的标记和代码:price我可以使用Jquery提交表单,但我不明白为什么this.form.submit()不能使用vanillajavascript?我正在使用Chrome对此进行测试。 最佳答案 div不是表单元素。它没有this.form。您仍然可以执行document.forms.form.submit()(.form因为您有name="form") 关于javascri

javascript - Angular2 - 类型错误 : Cannot read property 'Id' of undefined in (Typescript)

我收到以下错误:angular2.dev.js:23925EXCEPTION:TypeError:Cannotreadproperty'Id'ofnullin[{{product.Id}}inProductEditComponent@0:68]抛出://Product-edit.component.ts:import{Component}from'angular2/core';import{IProduct}from'./product'import{ProductService}from'./product.service'import{RouteParams}from'angula

javascript - 当您将 'this' 作为参数传递时?

这个问题在这里已经有了答案:Howdoesthe"this"keywordwork?(21个回答)6年前关闭。我正在尝试了解this,这让我有点困惑:varrandomFunction=function(callback){vardata=10;callback(data);};varobj={initialData:20,sumData:function(data){varsum=this.initialData+data;console.log(sum);},prepareRandomFunction:function(){randomFunction(this.sumData.b

javascript - For..in 循环 - 为什么它有效?

我正在阅读“面向网页设计师的JavaScript”这本书,我已经看到了这个例子:varfullName={"first":"John","last":"Smith"};for(varnameinfullName){console.log(name+":"+fullName[name]);}输出是:"first:John""last:Smith"我不明白的是:我要告诉程序在哪里获取字符串“first”和“last”。我的意思是,循环对象“fullName”,我看不出“name”如何与“first”和“last”相关。我希望这是清楚的。你能帮我吗?非常感谢! 最

javascript - JSON.parse() 导致错误 : `SyntaxError: Unexpected token in JSON at position 0`

我正在用Node.js编写我的第一个应用程序。我正在尝试从数据以JSON格式存储的文件中读取一些数据。我收到这个错误:SyntaxError:UnexpectedtokeninJSONatposition0atObject.parse(native)这是这部分代码://readsavedaddressesofallusersfromaJSONfilefs.readFile('addresses.json',function(err,data){if(data){console.log("ReadJSONfile:"+data);storage=JSON.parse(data);这是c

javascript - $(this) 和 this 有什么区别

我有以下代码$('a').click(function(){varurl=this.href;alert(url);});这工作得很好,果然返回的结果是一个标签的url。但是如果我把上面的代码改成$('a').click(function(){varurl=$(this).href;alert(url);});结果未定义。有人帮我解决这个问题吗?我为此苦苦思索.... 最佳答案 $(this)创建一个包装this的jQuery对象。nativeDOM对象具有href属性,但jQuery没有。$(this).attr("href")会

javascript - jQuery this.html() 返回未定义

我正在使用jQuery重新格式化一些非常糟糕的HTML。我需要拼接兄弟元素在一起。我试过这段代码:$('font+font').each(function(){this.html().appendTo(this.prev());this.remove();});但它给了我这个错误:TypeError:'undefined'isnotafunction(evaluating'this.html()')这是HTML的示例:Thisfragmentisactuallyoneelement.更新我用$(this)更新了我的代码,但它仍然无法正常工作。当我运行这段代码时$('font+font'