Namespacing technique in JavaScript, recommended? performant? issues to be aware of?

In a project I am working on I am structuring my code as follows MyLib = { AField:0, ASubNamespace:{ AnotherField:"value", AClass:function(param) { this.classField = param; this.classFunction = function(){ // stuff } } }, AnotherClass:function(param) { this.classField = param; this.cl

JavaScript中的命名空间技术,推荐? 高性能? 需要注意的问题?

在我正在开发的一个项目中,我正在构建我的代码,如下所示 MyLib = { AField:0, ASubNamespace:{ AnotherField:"value", AClass:function(param) { this.classField = param; this.classFunction = function(){ // stuff } } }, AnotherClass:function(param) { this.classField = param; this.classFunctio

Define global variable in a JavaScript function

Is it possible to define a global variable in a JavaScript function? I want use the trailimage variable (declared in the makeObj function) in other functions. <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> <script type="text/javascript"> var offsetfrommouse = [10, -20]; var d

在JavaScript函数中定义全局变量

是否可以在JavaScript函数中定义全局变量? 我想在其他函数中使用trailimage变量(在makeObj函数中声明)。 <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> <script type="text/javascript"> var offsetfrommouse = [10, -20]; var displayduration = 0; var obj_selected = 0;

JavaScript check if variable exists (is defined/initialized)

Which method of checking if a variable has been initialized is better/correct? (Assuming the variable could hold anything (string, int, object, function, etc.)) if (elem) { // or !elem or if (typeof(elem) !== 'undefined') { or if (elem != null) { You want the typeof operator. Specifically: if (typeof variable !== 'undefined') { // the variable is defined } The typeof operator will c

JavaScript检查变量是否存在(已定义/初始化)

检查变量是否已被初始化的哪种方法更好/更正? (假设变量可以容纳任何东西(string,int,object,function等)) if (elem) { // or !elem 要么 if (typeof(elem) !== 'undefined') { 要么 if (elem != null) { 你想要typeof运算符。 特别: if (typeof variable !== 'undefined') { // the variable is defined } typeof运算符将检查变量是否真的未定义。 if (typeof variable === 'undefined') { // varia

Check if a variable is a string in JavaScript

我怎样才能确定一个变量是一个字符串或JavaScript中的其他东西? You can use typeof operator: var booleanValue = true; var numericalValue = 354; var stringValue = "This is a String"; alert(typeof booleanValue) // displays "boolean" alert(typeof numericalValue) // displays "number" alert(typeof stringValue) // displays "string" Example from this webpage. (Example was slightly modified though). H

检查一个变量是否是JavaScript中的一个字符串

我怎样才能确定一个变量是一个字符串或JavaScript中的其他东西? 你可以使用typeof运算符: var booleanValue = true; var numericalValue = 354; var stringValue = "This is a String"; alert(typeof booleanValue) // displays "boolean" alert(typeof numericalValue) // displays "number" alert(typeof stringValue) // displays "string" 来自此网页的示例。 (虽然例子稍作修改)。 这里是typeof运算符的参考。 这

Why would a JavaScript variable start with a dollar sign?

I quite often see JavaScript with variables that start with a dollar sign. When/why would you choose to prefix a variable in this way? (I'm not asking about $('p.foo') syntax that you see in jQuery and others, but normal variables like $name and $order ) A very common use in jQuery is to distinguish jQuery objects stored in variables from other variables. For example, I would de

为什么JavaScript变量会以美元符号开头?

我经常看到带有以美元符号开头的变量的JavaScript。 何时/为什么要选择以这种方式为变量添加前缀? (我没有问你在jQuery和其他人看到的$('p.foo')语法,而是像$name和$order这样的普通变量) jQuery中非常常见的用法是将存储在变量中的jQuery对象与其他变量区分开来。 例如,我会定义 var $email = $("#email"); // refers to the jQuery object representation of the dom object var email_field = $("#email")

JavaScript plus sign in front of function name

I've been looking on info about self-invoking functions, and somewhere I stumbled on this notation: +function(){} Can someone explain to me what the + sign in front of the function means/does? It forces the parser to treat the part following the + as an expression. This is usually used for functions that are invoked immediately, eg: +function() { console.log("Foo!"); }(); Without the +

JavaScript加号在函数名前面

我一直在寻找关于自我调用函数的信息,以及我偶然发现这个符号的地方: +function(){} 有人可以向我解释功能前面的+号是什么意思吗? 它强制解析器将+的部分视为表达式。 这通常用于立即调用的函数,例如: +function() { console.log("Foo!"); }(); 如果没有+ ,那么如果解析器处于期望语句(可以是表达式或多个非表达式语句)的状态,那么function看起来像函数声明的开始而不是函数表达式,所以()在它后面(上面那行末

Javascript "this" pointer within nested function

I have a question concerning how the "this" pointer is treated in a nested function scenario. Say I insert this following sample code into a web page. I get an error when I call the nested function "doSomeEffects()". I checked in Firebug and it indicates that when I am in that nested function, the "this" pointer is actually pointing to the global "window&quo

嵌套函数中的Javascript“this”指针

我有一个关于如何在嵌套函数场景中处理“this”指针的问题。 假设我将下面的示例代码插入到网页中。 当我调用嵌套函数“doSomeEffects()”时出现错误。 我检查了Firebug,它表明当我在嵌套函数中时,“this”指针实际上指向全局的“窗口”对象 - 这是我没有想到的。 我不能正确地理解某些东西,因为我认为自从我在对象的函数中声明嵌套函数以来,它应该具有与该函数相关的“本地”范围(即,“this”指针将指向对象本身这是如何在我的

Coding Style Guide for node.js apps?

Is there a (or several) coding style guide for node.js? If not, what are the emerging styles used by the top open-source node projects? I'm looking for a guide (or several guides) along the lines of PEP 8, the canonical Coding Style Guide for Python. I've seen various JavaScript guides not worth linking here (mostly old and targeted at client-side JavaScript). I found one interesting

node.js应用的编码风格指南?

是否有一个(或几个)node.js的编码风格指南? 如果不是,那么最热门的开源节点项目使用哪些新兴的样式 ? 我正在寻找PEP 8(Python的标准编码风格指南)的指南(或几个指南)。 我已经看到各种JavaScript指南在这里不值得链接(大多数是旧的,针对客户端JavaScript)。 我找到了一个有趣的node.js风格指南。 编码风格指南或编码规范应包括(但不限于): 代码布局:缩进(2个空格,4个空格,制表符,...),换行符,换

How can I remove a style added with .css() function?

I'm changing CSS with jQuery and I wish to remove the styling I'm adding based on the input value: if(color != '000000') $("body").css("background-color", color); else // remove style ? How can I do this? Note that the line above runs whenever a color is selected using a color picker (ie. when mouse moves over a color wheel). 2nd note: I can't do this with css("background-co

如何删除使用.css()函数添加的样式?

我正在用jQuery更改CSS,并且希望根据输入值删除我要添加的样式: if(color != '000000') $("body").css("background-color", color); else // remove style ? 我怎样才能做到这一点? 请注意,只要使用颜色选择器选择颜色(即,当鼠标移动到色轮上时),上面的行就会运行。 第二个注意:我不能用css("background-color", "none")做到这一点,因为它会从css文件中删除默认样式。 我只想删除jQuery添加

One var per function in JavaScript?

I've been using JSLint to make me feel bad about my JavaScript. It is great, by the way. There is one check that I don't quite understand and I'd like your views, please. From jslint.com: In languages with block scope, it is usually recommended that variables be declared at the site of first use. But because JavaScript does not have block scope, it is wiser to declare all of a

JavaScript中的每个函数都有一个变量?

我一直在使用JSLint让我对我的JavaScript感觉不好。 顺便说一句,这很棒。 有一张支票我不太明白,我希望你的意见。 来自jslint.com: 在具有块范围的语言中,通常建议在首次使用的站点声明变量。 但是因为JavaScript没有块范围,所以在函数顶部声明一个函数的所有变量是明智的。 建议每个函数使用一个var语句。 大胆的最后一个表态是什么? 我想我应该像这样声明多个变量? var foo = 1, bar = 2; 而且,“明智的”