我在一个网站上工作,我得到了一个在Internet Explorer 6中不起作用的javascript函数.我知道
document.querySelector(selector)
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能让它在IE6中运行?
(我不会试图让它变得有趣;))
var a = "foo";
var c = Array.prototype.join.call( a, "-" ); // 'f-o-o'
Run Code Online (Sandbox Code Playgroud)
第二行代码如何工作?我没有看到任何字符串转换为数组然后再转换回来,这是在后台发生的吗?我遇到过这种代码,这很奇怪,一个接受字符串的数组方法.
此代码段中变量“元素”的类型是什么?我以为它是一个数字(ID 或其他东西),但现在,我不知道了。代码有效,但我不明白为什么可以在 for 循环中使用 var 元素,就像数组一样。对此有什么解释吗?
<script type="text/javascript">
function showAtrributes() {
var element = document.getElementById("videos");
var listAttributes = "";
for(var attribute in element) {
var valueOfAtrrib = element.getAttribute(attribute);
listAttributes = listAttributes + attribute + ": " + valueOfAttrib + "\n";
}
alert(listAttributes);
}
</script>
Run Code Online (Sandbox Code Playgroud) 我有许多功能方法我需要使用它,我需要发布一个使用这种方法的库与JavaScript开发人员分享它非常有用,所以例如我需要添加一个名为的方法duplicates 将返回给我的数组的重复项你可以看到ECMA没有正式发布这个方法所以我不知道放置脚本的最佳形式
1-
Array.prototype.duplicate = function (){
//script here its usefull to use `this` refer to the Array
}
Run Code Online (Sandbox Code Playgroud)
使用它就像
[1,2,2].duplicates();
Run Code Online (Sandbox Code Playgroud)
2-
var Ary = function(a){
if(!(this instanceOf Ary))
return new Ary(a)
if(Object.prototype.toString.call(a) != '[object Array]')
return new Error(a + 'is not an Array')
else
{
for(var i =0 ; i<a.length; i++)
{
this.push(a[i]);
}
}
}
Ary.prototype = new Array();
Ary.prototype.constructor = Ary;
Ary.prototype.duplicates = function(){
//script here its usefull to use `this` refer to the Array
}; …Run Code Online (Sandbox Code Playgroud) 我也可以,
var arr = [];
arr.forEach(function(i) {
i;
});
for (var i = 0, length = arr.length; i < length; ++i) {
arr[i];
}
Run Code Online (Sandbox Code Playgroud)
我应该何时使用另一个,是否存在性能差异?
我正在经历一个codeacademy的javascript练习并遇到了这个问题.codeacademy给出了以下代码.
var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "1",
address: ['abc', 'def', 'ghi']
},
steve: {
firstName: "Steve",
lastNAme: "Jobs",
number: "2",
address: ['abc', 'def', 'ghi']
}
};
var list = function(obj) {
for (var prop in obj) {
console.log(prop);
}
};
var search = function(name) {
for (var prop in friends) {
if (friends[prop].firstName === name) {
console.log(friends[prop]);
return friends[prop];
}
}
};
Run Code Online (Sandbox Code Playgroud)
我不明白的是,在搜索功能中,为什么我需要写出'friends [prop]'而不仅仅是'prop'.如果for/in循环遍历friends中的每个属性(数组?),为什么我需要再次指定每个prop所属的数组?为什么我不能使用以下代码?
var search = function(name) {
for (var prop in friends) …Run Code Online (Sandbox Code Playgroud) 我是一名新手,并试图创建js脚本来隐藏和在网页上显示可见的部分.以前的部分似乎工作正常.代码段的最后一部分返回此错误:
scripts.js:29 Uncaught TypeError: sections.indexOf is not a function
at nextSection (scripts.js:29)
at <anonymous>:1:1
Run Code Online (Sandbox Code Playgroud)
有谁能告诉我我没有到这里来的?
/*
============================================
array of all sections
============================================
*/
var sections=document.querySelectorAll("section");
/*
============================================
Function to find the section that is being displayed (the one that doesn't have "not1st" as a class.)
============================================
*/
function findSection(){
for (var i=0; i<sections.length; i++) {
if (sections[i].className.includes("not1st")){
continue;
} else {
return sections[i];
}}}
/*
============================================
Function to load the next section
============================================
*/
function nextSection() {
sections[sections.indexOf(findSection())+1];
}
Run Code Online (Sandbox Code Playgroud) 我想从对象数组中创建对象特定字段的数组。这就是我所做的。怎样才能更有效率呢?
var array_obectj = [
{ a: 'somestring1',
b: 42,
c: false},
{
a: 'somestring2',
b: 42,
c: false
}];
var arrayP = [];
for (var i in array_obectj){
arrayP.push(array_obectj[i].a)
}
Run Code Online (Sandbox Code Playgroud) 我正在for...in用 javascript编写一个简单的循环,想知道为什么key是字符串而不是数字?
为什么会这样,我可以将其更改为数字吗?
var array = ["a", "b", "c"];
for (var key in array) {
console.log(typeof key); //string
console.log(key + 1); //expected output : 01, 11, 21...
}
Run Code Online (Sandbox Code Playgroud) 我有一个错误TypeError: cars.map(...).then is not a function。我想在更新颜色时终止连接。
cars.map(function (item, i) {
if (item.color === 'red') {
updateColor(item.id)
}
}).then(() => connection.end())
Run Code Online (Sandbox Code Playgroud) javascript ×10
for-loop ×2
ecmascript-6 ×1
es6-promise ×1
foreach ×1
indexof ×1
node.js ×1
promise ×1