Well, first I should probably ask if this is browser dependent. I've read that if an invalid token is found, but the section of code is valid until that invalid token, a semicolon is inserted before the token if it is preceded by a line break. However, the common example cited for bugs caused by semicolon insertion is: return _a+b; ..which doesn't seem to follow this rule, since _
那么,首先我应该问问这是否依赖于浏览器。 我读过如果找到无效标记,但代码段直到该无效标记才有效,则在标记之前插入分号(如果它前面有换行符)。 但是,引用由分号插入引起的错误的常见示例是: return _a+b; ..这似乎不遵循这个规则,因为_a将是一个有效的标记。 另一方面,打破呼叫连锁按预期工作: $('#myButton') .click(function(){alert("Hello!")}); 有没有人有更深入的规则描述? 首先,您应该知
I'm writing some JavaScript code to parse user-entered functions (for spreadsheet-like functionality). Having parsed the formula I could convert it into JavaScript and run eval() on it to yield the result. However, I've always shied away from using eval() if I can avoid it because it's evil (and, rightly or wrongly, I've always thought it is even more evil in JavaScript, becaus
我正在编写一些JavaScript代码来解析用户输入的函数(用于类似电子表格的功能)。 解析了公式之后,我可以将它转换成JavaScript并在其上运行eval()以产生结果。 但是,如果我可以避免使用eval() ,因为它是邪恶的(而且,无论正确还是错误,我一直认为它在JavaScript中更加邪恶,因为要评估的代码可能会更改由用户)。 那么,什么时候可以使用它? 我想花一点时间来解决你的问题的前提--eval()是“邪恶的”。 编程语言用
I have a javascript file that reads another file which may contain javascript fragments that need to be eval()-ed. The script fragments are supposed to conform to a strict subset of javascript that limits what they can do and which variables they can change, but I want to know if there is some way to enforce this by preventing the eval from seeing variables in the global scope. Something like t
我有一个JavaScript文件,读取另一个文件,其中可能包含需要eval() - ed的JavaScript片段。 脚本片段应该符合javascript的严格子集,这限制了他们可以做的事情以及他们可以更改哪些变量,但是我想知道是否有某种方法可以通过阻止eval在全局范围中看到变量来强制执行此操作。 如下所示: function safeEval( fragment ) { var localVariable = g_Variable; { // do magic scoping here so that the eval fr
I used to know what this meant, but I'm struggling now... Is this basically saying document.onload ? (function () { })(); It's an Immediately-Invoked Function Expression, or IIFE for short. It executes immediately after it's created. It has nothing to do with any event-handler for any events (such as document.onload ). The first pair of parentheses (function(){...}) turns th
我曾经知道这意味着什么,但我现在正在努力... 这基本上是说document.onload ? (function () { })(); 它是立即调用的函数表达式,简称IIFE 。 它在创建后立即执行。 它与任何事件的任何事件处理程序(如document.onload )无关。 第一对括号(function(){...})将代码(在这种情况下是一个函数)转换为一个表达式,第二对括号(function(){...})()调用该评估表达式产生的函数。 当试图避免污染全局名称空间时,通常会
This question already has an answer here: What is the scope of variables in JavaScript? 25 answers No, the variable a in your function is not visible outside of the function scope. If you try to access the variable a somewhere else in the code it will display undefined , because you have just declared the variable, but didn't assign any value to it. Let's extend your example: var
这个问题在这里已经有了答案: JavaScript中变量的范围是什么? 25个答案 不,函数中的变量a在函数范围外是不可见的。 如果您尝试访问该变量a中的代码会显示在其他地方undefined ,因为你刚才声明的变量,但没有任何值分配给它。 让我们来扩展你的例子: var a; function myFunction() { var a = 4; } function anotherFunction() { console.log(a); } anotherFunction(); > undefined 当我们调用anotherFu
var e = 15; function change_value(e){ e = 10; } change_value(e); console.log(e); e的价值仍然是15。 The e inside the function scope is different from the e inside the global scope. Just remove the function parameter: var e = 15; function change_value(){ e = 10; } change_value(); console.log(e); javascript does not use reference for simple types. It use a copy method instead. S
var e = 15; function change_value(e){ e = 10; } change_value(e); console.log(e); e的价值仍然是15。 该e功能范围内从不同e全局范围内。 只需删除函数参数: var e = 15; function change_value(){ e = 10; } change_value(); console.log(e); JavaScript不使用简单类型的引用。 它使用复制方法。 所以你不能这样做。 你有2个解决方案。 这条路 : var e = 15; function change_value(e) { ret
I have the code below in the beggining of my JS scirpt. When I try to print any of defined variables below that code, the console says that variables are not defined. Can you tell me why? Maybe I should call the funtion first? When I define variable below and then print it to the console, it works fine. Here is the code: document.addEventListener("DOMContentLoaded", function(){ var h
我在下面的代码在我的JS scirpt的开始。 当我尝试在该代码下面打印任何定义的变量时,控制台说变量没有被定义。 你能告诉我为什么吗? 也许我应该先打电话给功能? 当我在下面定义变量并将其打印到控制台时,它可以正常工作。 代码如下: document.addEventListener("DOMContentLoaded", function(){ var homeElement = document.getElementById("home"); var childElements = document.querySelector(".oferts
This question already has an answer here: Why does jQuery or a DOM method such as getElementById not find the element? 6 answers 在创建所有元素时,必须将脚本放置在文档的末尾: <!DOCTYPE html> <html> <head> </head> <body> <p>I am a paragraph tag</p> <h1 >I am an h1 tag</h1> <div id="id"> I am a div tag</div> </b
这个问题在这里已经有了答案: 为什么jQuery或像getElementById这样的DOM方法找不到元素? 6个答案 在创建所有元素时,必须将脚本放置在文档的末尾: <!DOCTYPE html> <html> <head> </head> <body> <p>I am a paragraph tag</p> <h1 >I am an h1 tag</h1> <div id="id"> I am a div tag</div> </body> <script src='test.js'>
I've read this answer, reducing boilerplate, looked at few GitHub examples and even tried redux a little bit (todo apps). As I understand, official redux doc motivations provide pros comparing to traditional MVC architectures. BUT it doesn't provide an answer to the question: Why you should use Redux over Facebook Flux? Is that only a question of programming styles: functional vs n
我读过这个答案,减少了样板,查看了几个GitHub的例子,甚至尝试了一点点(todo应用程序)。 据我所知,官方的redux doc动机提供了与传统MVC架构相比的优点。 但它没有提供对这个问题的答案: 为什么你应该使用Facebook上的Flux使用Redux? 这只是一个编程风格的问题:功能与非功能? 或者问题出在redux方法中的能力/开发工具? 也许缩放? 还是测试? 如果我说,redux对于来自功能语言的人来说是一种流量,我是对的
I've been learning Javascript using Object-Oriented JavaScript by Stoyan Stefanov He offers an example comparing global and local scope: var a = 123; function f() { alert(a); var a = 1; alert(a); } f(); Looking at this example, I expected the first alert to be '123' and the second alert to be '1'. Lo and behold, Stoyan says: You might expect that the first a
我一直在使用Stoyan Stefanov使用面向对象的JavaScript学习Javascript 他举了一个比较全球和本地范围的例子: var a = 123; function f() { alert(a); var a = 1; alert(a); } f(); 看看这个例子,我预计第一个警报是'123',第二个警报是'1'。 你看,斯托扬说: 您可能预期第一个alert()将显示123(全局变量a的值),第二个将显示1(本地a)。 不是这种情况。 第一个警报将显示“未定义”