Why does a child object in JavaScript lose the global scope?

I am striving to follow Douglas Crockford's advice from "JavaScript: The Good Parts" and his web site: Use of global variables should be minimized. Implied global variables should never be used. To do this, I have defined one "root" object that acts as a container for all other objects, and everything is now arranged into a logical hierarchy of shared functionality.

为什么JavaScript中的子对象会失去全局范围?

我努力遵循Douglas Crockford的“JavaScript:The Good Parts”的建议和他的网站: 应尽量减少全局变量的使用。 隐含的全局变量决不应该被使用。 为此,我定义了一个“根”对象,它充当所有其他对象的容器,现在所有东西都被安排到共享功能的逻辑层次结构中。 我陷入困境的地方是儿童对象似乎失去了全球对象的范围。 我能想到的最好的例子就是我的记录器,我想将其全局定义为root.log ,并在其他地方重复使用它。 但是,当

How to get javascript object method name?

I'm currently building a bigger object and need to get faster and more specific with debugging/inspecting values/results/returns. Now I thought about the following: var myObject = { whatever: null, whereever: null, debug: false, someFunction: function( arg ) { // General - would output *all* logs for all object functions. // if ( true === myObject.debug )

如何获取javascript对象的方法名称?

我目前正在构建一个更大的对象,并且需要通过调试/检查值/结果/返回来更快,更具体。 现在我想到了以下几点: var myObject = { whatever: null, whereever: null, debug: false, someFunction: function( arg ) { // General - would output *all* logs for all object functions. // if ( true === myObject.debug ) // console.log( 'FunctionNameHere', arg ); // Mor

Does JavaScript Guarantee Object Property Order?

If I create an object like this: var obj = {}; obj.prop1 = "Foo"; obj.prop2 = "Bar"; Will the resulting object always look like this? { prop1 : "Foo", prop2 : "Bar" } That is, will the properties be in the same order that I added them? No, properties order in objects is not guaranteed in JavaScript; you need to use an Array . Definition of an Object from ECMAScript Third Edition (pdf):

JavaScript保证对象属性顺序?

如果我创建一个像这样的对象: var obj = {}; obj.prop1 = "Foo"; obj.prop2 = "Bar"; 产生的对象是否总是像这样? { prop1 : "Foo", prop2 : "Bar" } 也就是说,这些属性的排列顺序是否与我添加的顺序相同? 不,在JavaScript中不保证对象中的属性顺序; 你需要使用一个Array 。 ECMAScript第三版中对象的定义(pdf): 4.3.3对象 一个对象是Object类型的成员。 它是一个无序的属性集合,每个属性都包含一个原始值

Is this a good pattern?

Objectives... Remove vars, objects etc from the global object. Remove possibility of collisions. Firstly I implement the Yahoo namespace code (note for example purposes I am using ROOT as the root of my namespace)... if (typeof ROOT == "undefined" || !ROOT) { var ROOT = {}; } ROOT.namespace = function () { var a = arguments,

这是一个很好的模式?

目标... 从全局对象中删除变量,对象等。 消除碰撞的可能性。 首先,我实现了Yahoo命名空间代码(注意,例如,我使用ROOT作为我的命名空间的根目录)... if (typeof ROOT == "undefined" || !ROOT) { var ROOT = {}; } ROOT.namespace = function () { var a = arguments, o = null, i, j, d; for (i = 0; i < a.le

How can I pass a parameter to a setTimeout() callback?

I have some JavaScript code that looks like: function statechangedPostQuestion() { //alert("statechangedPostQuestion"); if (xmlhttp.readyState==4) { var topicId = xmlhttp.responseText; setTimeout("postinsql(topicId)",4000); } } function postinsql(topicId) { //alert(topicId); } I get a error that topicId is not defined Everything was working before i used the setTimeout() func

我怎样才能传递参数给setTimeout()回调?

我有一些JavaScript代码,如下所示: function statechangedPostQuestion() { //alert("statechangedPostQuestion"); if (xmlhttp.readyState==4) { var topicId = xmlhttp.responseText; setTimeout("postinsql(topicId)",4000); } } function postinsql(topicId) { //alert(topicId); } 我得到一个错误, topicId没有被定义在我使用setTimeout()函数之前,一切都在工作。 我想在一段时间后调用postinsq

Build an HTML5 game

This question already has an answer here: What does “var FOO = FOO || {}” (assign a variable or an empty object to that variable) mean in Javascript? 7 answers || is the "or" operator in JavaScript. It returns the value on the left if the value on the left is truthy, otherwise it returns the value on the right. undefined and null , are both falsey values in JavaScript, so if wi

构建一个HTML5游戏

这个问题在这里已经有了答案: 什么是“var FOO = FOO ||” {}“(分配一个变量或一个空对象的变量)意味着在Javascript中? 7个答案 || 是JavaScript中的“或”运算符。 如果左边的值是truthy,它将返回左边的值,否则返回右边的值。 undefined和null都是JavaScript中的错误值,所以如果window.BubbleShoot是其中之一,则代码的第一行将设置BubbleShoot的值为{} ,这是一个空的JavaScript对象,您可以在其中设置属性像第二

Javascript object creation?

Possible Duplicate: What does “var FOO = FOO || {}” mean in Javascript? I am finding this kind of statement in javascript object creation repeatedly. var MyObj = MyObj || {}; Could some one could explain the significance of the above statement? why can't we create just var MyObj = {}; Thanks. var MyObj = MyObj || {}; That simply says "if MyObj already exists and has a truth

Javascript对象创建?

可能重复: 什么是“var FOO = FOO ||” {}“的意思是在Javascript中? 我反复在javascript对象创建中找到这种声明。 var MyObj = MyObj || {}; 有人可以解释上述说法的意义吗? 为什么我们不能创造 var MyObj = {}; 谢谢。 var MyObj = MyObj || {}; 这只是说“如果MyObj已经存在并且具有真值,保留它;否则,创建一个新对象”。 例如,这是为功能做可选参数的常用方法。 有关该主题的更多信息,请参阅MDN关于逻辑运

{}" in Javascript

This question already has an answer here: What does “options = options || {}” mean in Javascript? [duplicate] 5 answers What does “var FOO = FOO || {}” (assign a variable or an empty object to that variable) mean in Javascript? 7 answers In browsers, the window object is global. If $N is falsy, {} will be assigned to window.$N . If $N is truthy, its value will be assigned to window.$

{}“

这个问题在这里已经有了答案: 什么是“选项=选项||” {}“的意思是在Javascript中? [重复] 5个答案 什么是“var FOO = FOO ||” {}“(分配一个变量或一个空对象的变量)意味着在Javascript中? 7个答案 在浏览器中, window对象是全局的。 如果$N是虚假的, {}将被分配给window.$N 如果$N是真的,它的值将被赋值给window.$N (可能保持相同的值)。 这是覆盖默认值的常用方法。

What does 'this.x1 = options.x1

Possible Duplicates: What does "options = options || {}" mean in Javascript? null coalescing operator for javascript? Was reading some code and I saw this: this.x1 = options.x1 || 0; Never seen syntax like this before. What does it mean? 这是一个合并......这意味着如果options.x1是虚假的,它将分配0,否则就是options.x1。 Let this.x1 be the value of options.x1 if options.x1 has

'this.x1 = options.x1是什么?

可能重复: Javascript中的“options = options || {}”是什么意思? null的合并运算符的JavaScript? 正在阅读一些代码,我看到了这一点: this.x1 = options.x1 || 0; 以前从未见过这样的语法。 这是什么意思? 这是一个合并......这意味着如果options.x1是虚假的,它将分配0,否则就是options.x1。 如果options.x1有任何真值,则让this.x1为options.x1值。 否则,让this.x1为0 。

" mean in javascript array creation?

This question already has an answer here: What does “var FOO = FOO || {}” (assign a variable or an empty object to that variable) mean in Javascript? 7 answers It checks if _array is defined, otherwise, it assigns an array to it. Basically a "Use existing or assign new" scenario. The second line can then run safely since _array is (presumably) an existing array, or a newly crea

“意味着在JavaScript数组创建?

这个问题在这里已经有了答案: 什么是“var FOO = FOO ||” {}“(分配一个变量或一个空对象的变量)意味着在Javascript中? 7个答案 它检查_array是否被定义,否则它会为其分配一个数组。 基本上是“使用现有的或分配新的”方案。 第二行然后可以安全地运行,因为_array (大概)是现有的数组,或者是新创建的数组,由第一行提供。 它意味着or 。 在这种情况下, get _array variable or create new empty array if _arr