有人知道有一种方法可以在 JavaScript 对象文本中引用同级键的值吗?
target所以在这里使用函数中的值beforeNext():
obj: {
target: 'li.player a.icon-tag',
parent: 'ul#drop_list',
beforeNext: function(){
target.addClass('bind active');
}
}
Run Code Online (Sandbox Code Playgroud) javascript object-literal key-value siblings cross-reference
我陷入困境,因为我需要选择性能最佳的选项。
我现在拥有的是一个简单的数组,例如:
var array = [
'/index1.html',
'/index2.html',
'/index3.html'
];
Run Code Online (Sandbox Code Playgroud)
该数组仅包含大约 60 个选项,但由于我需要按语言分隔,所以我正在考虑其他选项,例如对象文字或 JSON 格式,因此它将包含所有语言和大约 1000 个选项。
var obj = {
'en' : {
'title' : 'bla',
'url':'bla bla bla'
},
'de' : {
'title' : 'bla',
'url':'bla bla bla'
},
};
Run Code Online (Sandbox Code Playgroud)
问题是你认为什么最适合这个?谢谢。
var x = {
name: "japan",
age: 20
}
x.prototype.mad = function() {
alert("USA");
};
x.mad();
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用。对象字面量不能扩展?或x.mad()呼叫方式不正确。
我有一个看起来像这样的对象
{
"_id": "DEADBEEF",
"_rev": "2-FEEDME",
"name": "Jimmy Strawson",
"link": "placeholder.txt",
"entries": {
"Foo": 0
}
}
Run Code Online (Sandbox Code Playgroud)
通过$ .getJSON调用将其读入我的javascript.
所以我有JS对象"回复",它包含所有这些数据.
我需要附加项目,以便"条目"扩展如下:
{
"_id": "DEADBEEF",
"_rev": "2-FEEDME",
"name": "Jimmy Strawson",
"link": "placeholder.txt",
"entries": {
"Foo": 0,
"Bar": 30,
"Baz": 4
}
}
Run Code Online (Sandbox Code Playgroud)
我试过了
reply['entries'].push({"Bar": 0});
Run Code Online (Sandbox Code Playgroud)
但这不起作用(我认为因为什么都不是数组)
有人可以提供替代方法吗?
如果我输入以下内容:
interface A {
x: number
}
interface B {
y: number
}
type Z = A | B;
// here it allows me to create a variable of type Z with both members of type A and B.
let z: Z = {
x: 5,
y: 6,
}
Run Code Online (Sandbox Code Playgroud)
我无法确保 Z 类型的对象确实包含 A 的所有成员但不包含 B 的成员(或相反)。使用 TypeScript 有可能吗?经过大量研究,我倾向于“不”的答案,但我不确定。
为什么我用这段代码得到这个错误:Uncaught SyntaxError: Unexpected token {在第1行.
var cube_points = {
{'x' : 100, 'y' : 100, 'z' : 100},
{'x' : 100, 'y' : 100, 'z' : -100},
{'x' : -100, 'y' : 100, 'z' : -100},
{'x' : -100, 'y' : 100, 'z' : 100},
{'x' : 100, 'y' : -100, 'z' : 100},
{'x' : 100, 'y' : -100, 'z' : -100},
{'x' : -100, 'y' : -100, 'z' : -100},
{'x' : -100, 'y' : -100, …Run Code Online (Sandbox Code Playgroud) 在我的Javascript应用程序中,我有一个Object,我需要能够通过内部对象中的值对数组进行排序.
例如:
{
a : {
timestamp: xxxxxx
other : yyyyyy
},
b : {
timestamp: xxxxxx
other : yyyyyy
},
c : {
timestamp: xxxxxx
other : yyyyyy
}
}
Run Code Online (Sandbox Code Playgroud)
我需要做的是管理这个数据集并根据每个内部对象的时间戳重新排序数组.
他们有什么方法可以做到这一点?
更新:
我最初的想法是做这样的事情:
{
a : {},
b : {},
c : {},
_ : [
c, a, b //Key's Only
]
}
Run Code Online (Sandbox Code Playgroud)
然后根据这些值重新索引对象,这将排序如何索引对象,但是当我插入一个新元素时,我还必须重新生成_索引关系,这似乎很费力.
我在JS中创建了一个对象文字,但是想要在不使用函数的情况下访问属性.每当我尝试,我都没有得到任何结果或它返回null.
JS:
var settings = {
prev: '#prev',
next: '#next',
slides: '#slides',
crslContainer: '#carousel',
//I'm trying to access the 'slides' by using 'this.slides'
inDent: $(this.slides).find('li').outerWidth(),
fx: 'rotate',
myFunction: function () {
console.log(settings.inDent); //This shows null in the console, why??
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我试图slides通过使用this.slides,在同一个对象内部访问,但控制台说null.但是当我做这个方法时inDent: $('#slides').find('li').outerWidth(),它就可以了.这是为什么?我假设它与使用this关键字在函数内使用这些属性相同,显然情况并非如此.我只想将该对象属性(即字符串)的值传递给另一个不是函数的属性.
非常感谢
我正在阅读有关Javascript Objects(http://javascriptissexy.com/javascript-objects-in-detail/)的文章,因此我将以下代码复制粘贴到我的Notepad ++中并在Chrome中运行(版本55.0.2883.87 m) .打开控制台后,控制台会报告SyntaxError.有人知道为什么吗?一切似乎都好.
// We have been using dot notation so far in the examples above, here is another example again:?
?var book = {title: "Ways to Go", pages: 280, bookMark1:"Page 20"};
?
?// To access the properties of the book object with dot notation, you do this:?
console.log(book.title); // Ways to Go?
console.log(book.pages); // 280
Run Code Online (Sandbox Code Playgroud) 这是一个简化的示例:
class PersonParms{
name:string;
lastName:string;
age?:number;
get fullName(){return this.name + " "+this.lastName;}
}
class Person{
constructor(prms:PersonParms){
}
}
new Person({name:'John',lastName:'Doe'}) // ts error: Property 'fullName' is missing in type '{ name: string; lastName: string; }'.
Run Code Online (Sandbox Code Playgroud)
这个想法是将一个文字对象作为PersonParms的inizizalizer传递,但是拥有该getter既不能声明该getter可选,也不能将该属性添加到对象文字中。还有另一种方法可以实现吗?
object-literal ×10
javascript ×8
arrays ×3
jquery ×3
typescript ×2
console.log ×1
getter ×1
json ×1
key-value ×1
map ×1
node.js ×1
object ×1
optional ×1
prototype ×1
siblings ×1
sorting ×1
syntax-error ×1
types ×1