我正在使用自调用匿名函数来同时创建对象和实例.最后创建的对象将覆盖第一个对象的属性.这是为什么?
<script>
LF = '<br/>'; //line feed
// object a with property name
!function () {
window.a = this; // make global object
this.name = 'a';
document.write('inside: a.name=' + this.name + LF);
}();
// object b with property name
!function () {
window.b = this; // make global object
this.name = 'b';
document.write('inside: b.name=' + this.name + LF);
}();
document.write('outisde: ' + ' a.name=' + a.name + ' b.name=' + b.name + LF);
</script>
Run Code Online (Sandbox Code Playgroud)
结果:
inside: a.name=a
inside: b.name=b
outisde: …Run Code Online (Sandbox Code Playgroud) 请考虑以下JavaScript类,属性和方法:
function Foo() {
this.data = 123;
}
Test.prototype.foo = function() {
$("body").append($("<div>")
.text("Hello World!")
.click(function () {
alert("Data: " + this.data);
})
);
}
var Bar = new Foo();
Bar.foo();
Run Code Online (Sandbox Code Playgroud)
为什么data警报框中的属性未定义?如何引用"this",如同指向Foo类而不是jQuery div元素?
如果我们创建一些对象,并用这些对象填充数组,那么名称是存储在数组中还是仅存储在对象的属性中?我想这可能是微不足道的,但我一直找不到答案。
var boxA = {color: "red", width: 100};
var boxB = {color: "yellow", width: 200};
var boxC = {color: "blue", width: 300};
boxArray = [boxA, boxB, boxC];
for (var i = 0; i < boxArray.length; i++) {
//****
// What code do we insert here to log
// boxA
// boxB
// boxC
//****
}
Run Code Online (Sandbox Code Playgroud)
当然,添加一个简单的解决方法
boxA.box = boxA;
Run Code Online (Sandbox Code Playgroud)
等等,然后打电话
console.log(boxArray[i].box);
Run Code Online (Sandbox Code Playgroud)
但这真的有必要吗?
试图推入新的嵌套对象.继续cannot read property push of undefined上线3.
为什么这不起作用?我应该这样做吗?
$scope.item.deliverables[0].steps[0].versions = [];
$scope.item.deliverables[0].steps[0].versions.push({assets:[{url:'aaa'}]})
$scope.item.deliverables[0].steps[0].versions.assets.push({url:'bbb'})
Run Code Online (Sandbox Code Playgroud) 我创建了一个类似于以下的javascript函数:
function highlight_input() {
$("input").css("background", "yellow");
}
$("#input1").highlight_input();
$("#input2").highlight_input();
Run Code Online (Sandbox Code Playgroud)
现在我想在需要时突出显示两种输入类型!但两者都不应该一次突出!
我有像行的数据
1. {type:"pant", service:"normal", price:"30"}
2. {type:"pant", service:"premium 1", price:"50"}
3. {type:"pant", service:"premium 2", price:"70"}
4. {type:"pant", service:"premium 3", price:"100"}
Run Code Online (Sandbox Code Playgroud)
寻找类似的东西
var x = {"pant", [{service:"normal", price:"30"},{service:"premium 1", price:"50"} ,{service:"premium 2", price:"70"}, {service:"premium 3", price:"100"}]}
Run Code Online (Sandbox Code Playgroud)
这样我就可以通过x ['喘气']获得所有类型的服务和价格
我有一个对象,我需要找到一个特定的项目索引号.以下是我的目标:
[
{
"type": "Grayscale",
"mode": "average"
}, {
"type": "Sepia"
}, {
"type": "Invert",
"invert": true
}, {
"type": "Convolute",
"opaque": false,
"matrix": [1, 1, 1, 1, 0.7, -1, -1, -1, -1]
}, {
"type": "Convolute",
"opaque": false,
"matrix": [0, -1, 0, -1, 5, -1, 0, -1, 0]
}, {
"type": "Brownie"
}, {
"type": "Brightness",
"brightness": 0.35
}
]
Run Code Online (Sandbox Code Playgroud)
例如,我需要找到具有Invert该type属性值的项的索引号.所以在这种情况下,输出应该是2.我只需要搜索type键的值.
让我们假设我们有一个profile具有属性name和getName方法的对象(箭头函数).
profile = {
name: 'abcd',
getName: () => {
console.log(this.name);
}
}
Run Code Online (Sandbox Code Playgroud)
我想getName通过保持箭头功能完整,而不是将其更改为常规功能来调用方法.
如何abcd通过调用获得输出getName().
你可以在里面添加表达式getName.
请问call()或bind()帮助?如果是这样,怎么样?
请勿将箭头功能更改为常规功能
- 已编辑 -
我只想问一下如何在对象内部使用箭头函数,以便它反映结果,因为我们将进入常规函数.
这只是一个采访问题.
我在JS中声明了一个对象,并尝试为其属性赋值.但是当我只定义了一个属性但没有多个属性时,我可以这样做.
这很好用:
let User = {
name
};
User['name']='Praveen';
alert(User.name);Run Code Online (Sandbox Code Playgroud)
但事实并非如此
let User = {
name,
email
};
User['name']='Praveen';
User['email']='incopraveen@gmail.com';
alert(User.email); //says email is not defined.Run Code Online (Sandbox Code Playgroud)
注意:我也试过删除分号.也试过点符号
假设我有一个这样的列表结构:
[
"id1", "01-01-2019", "name1", "€ 5,60",
"id2", "02-01-2019", "name2", "€ 5,70",
"id3", "03-01-2019", "name3", "€ 5,20",
...
]
Run Code Online (Sandbox Code Playgroud)
我想将它转换为具有json对象的列表,如下所示:
[
{
"id": "id1",
"date": "01-01-2019",
"name": "name1",
"value": "€ 5,60"
},
{
"id": "id2",
"date": "02-01-2019",
"name": "name2",
"value": "€ 5,70"
},
{
"id": "id3",
"date": "03-01-2019",
"name": "name3",
"value": "€ 5,20"
},
...
]
Run Code Online (Sandbox Code Playgroud)
在JavaScript中执行此任务的最有效方法是什么?