JS教程:循环语句
1、while循环写法如下:基本上和c++写法相同
var i = 0;
while (++i <= 10 ){
console.log("i = " + i);
}
2、for循环的写法如下:基本上和c++的写法一样。
for (var i =1; i<= 4; i++) {
console.log(" i = " + i);
}
3、for的另外一个用法c++是没有的。这个写法和python的写法比较像。
function Test(){
this.name = "test";
this.num = 14;
this.func = function(){
console.log("func")
}
}
var test = new Test();
for(keys in test){
console.log(test[keys])
}
4、js的forEach不是关键字而是一个接口。接口形式如下:
array.forEach(function(currentValue, index, arr), thisValue)
例如:
var numbers = [123, 12344, 123123, 123];
function func(i) {
console.log(" i = " +i);
}
numbers.forEach(func)
5、还用一种循环是用for .. of。这个倒是不常见。
var str = "test";
for (var s of str) {
console.log(s);
}
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:28
阅读量:44
阅读量:39
阅读量:71
阅读量:20