这是一个样本.我的担心似乎都在做.哪个更喜欢?
var fooArr = [{ "bar": {"blah": 9 } }];
for(var i in fooArr) {
var value = fooArr[i].bar.blah;
console.log(value); //checking in firebug console
}
var fooObj = { "bar": {"blah": 9 } };
for(var i in fooObj) {
var value = fooObj[i].blah;
console.log(value); //checking in firebug console
}
Run Code Online (Sandbox Code Playgroud)
此外,以下似乎无效,任何避免数组表示法的方法.
var fooObj1 = {
{ "bar": { "blah": 9 } },
{ "bar": { "blah": 4 } },
{ "bar": { "blah":12} }
};
Run Code Online (Sandbox Code Playgroud)
所以我不得不将上面的代码修改为类似下面的代码.使用Object文字过于粘稠是不是太糟糕了
var fooObj1 = {
1:{ …Run Code Online (Sandbox Code Playgroud) 如何在文字对象中声明私有var?我保留了这段代码:
var foo = {
self: null,
init: function() {
self = this;
self.doStuff();
},
doStuff: function() {
//stuff here
}
}
Run Code Online (Sandbox Code Playgroud)
这非常有效,但是如果我有一些对象,var"self"它将覆盖..显然它是一个全局变量..我不能在这个对象中使用受限制的单词"var".
我该如何解决这个问题,或者为每个对象创建一个NameSpace?
谢谢!
我需要遍历一系列DOM元素并使用jQuery创建一个对象文字,如下所示.我猜这涉及到.each的使用,但我有点不知道接下来要做什么.
'1': {
'text': 'Maintain Compliance',
'desc': 'blah',
'size': 10,
'color': '#afb0b3'
},
'2': {
'text': 'lorem ipsum',
'desc': 'blah.',
'size': 4,
'color': '#9b9ca0'
},
'3': {
'text': 'lorem ipsum',
'desc': 'blah',
'size': 6,
'color': '#c5c6c7'
}
Run Code Online (Sandbox Code Playgroud) 该线程描述了如何使用Javascript obect文字表示法来描述函数集合,例如:
var crudActions = {
create : function () {
...
}
read : function () {
...
}
}
Run Code Online (Sandbox Code Playgroud)
这个模式有名字吗?使用这种方法有优势吗?
我在javascript中看到了以下两种为对象文字指定属性的方法.
var a = {prop:2}
Run Code Online (Sandbox Code Playgroud)
和
var a ={'prop':2}
Run Code Online (Sandbox Code Playgroud)
这两种方式有什么区别..我知道第二种方法允许我们在属性名称中包含空格.还有其他任何优点吗?
换句话说,让值为变量.像这样的东西:
var a=3,b=4;
var obj = {x : a, y : b};
alert(obj.x); //will display '3'
a=2;
alert(obj.x); //want it to display '2' - how can i accomplish this?
Run Code Online (Sandbox Code Playgroud) 使用这样的文字符号声明一个对象:
var person = {
name: "",
gender: "",
age: 0
}
Run Code Online (Sandbox Code Playgroud)
与像这样的构造函数函数:
var person = function(name, gender, age)
{
this.name = name;
this.gender = gender;
this.age = age;
}
Run Code Online (Sandbox Code Playgroud)
第一个问题:
当这样声明时,即使它们还没有“实例化”,它们是否都占用了相等的内存量?(或者这个概念不适用于 JavaScript)
第二个问题:
这两个都可以正确更新如下:
var john = new person();
Run Code Online (Sandbox Code Playgroud) 是否可以在javascript对象文字中添加注释?以下示例适用于Firefox,但我找不到任何明确的文档.此外,我看到的所有示例似乎都避免了对象文字中的注释.
var o = {
p1: 2, // a comment about p1
/* A comment about function f1 */
f1: function() {
return 3;
}
};
Run Code Online (Sandbox Code Playgroud) 假设我在Scala.js中具有此定义,用于我正在使用的某些库的函数的返回类型:
@native
trait Link extends Object {
val href: String = native
val title: String = native
}
Run Code Online (Sandbox Code Playgroud)
在Scala代码中,定义符合此要求的对象文字的最佳,类型安全的方法是什么?我认为这个use(...).as[...]技巧会起作用:
val link = js.use(new {
val href = "value1"
val title = "value2"
}).as[Link]
Run Code Online (Sandbox Code Playgroud)
但这会产生错误:
AnyRef {val href:字符串;val title:String}不会导出getter href:String
为什么?
我也尝试过此方法,并且按预期失败了:
val link = js.use(js.Dynamic.literal(
href = cap(2),
title = cap(3)
)).as[Link]
Run Code Online (Sandbox Code Playgroud)
给
具有scala.scalajs.js.Dynamic的scala.scalajs.js.Object没有getter href:字符串
最后,我还尝试了以下方法:
val link = new Link {
override val href = "value1"
override val title = "value2"
}
Run Code Online (Sandbox Code Playgroud)
并得到
Scala.js定义的JS类无法直接扩展本机JS特性
现在,我正在这样做:
val link …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的开关箱考虑任何替代方案,因为它很长我嵌套的开关箱看起来像这样
switch (currentTab) {
case pending:
switch (status) {
case approved:
case denied:
break;
case partialApproved:
finalUpdatedInvoiceAmount = updatedInvoiceAmount - (feeAmount - (approvedFeeAmount || 0));
break;
}
break;
case approved:
switch (status) {
case denied:
case partialApproved:
finalUpdatedInvoiceAmount = updatedInvoiceAmount - (feeAmount - (approvedFeeAmount || 0));
break;
}
break;
case denied:
switch (status) {
case approved:
case partialApproved:
finalUpdatedInvoiceAmount = oldApprovedFeeAmount ? updatedInvoiceAmount + (feeAmount - oldApprovedFeeAmount) : updatedInvoiceAmount - (feeAmount - (approvedFeeAmount || 0));
break;
}
break;
case partiallyApprovedTab: …Run Code Online (Sandbox Code Playgroud) javascript object-literal switch-statement typescript angular
object-literal ×10
javascript ×8
angular ×1
casting ×1
each ×1
function ×1
jquery ×1
json ×1
object ×1
private ×1
properties ×1
scala.js ×1
type-safety ×1
typescript ×1
var ×1