JS教程:循环语句

 时间:2026-04-30 12:05:02

1、while循环写法如下:基本上和c++写法相同

var i = 0;


while (++i  <= 10 ){
   console.log("i = " + i);
}

JS教程:循环语句

2、for循环的写法如下:基本上和c++的写法一样。

for (var i =1; i<= 4; i++) {


   console.log("    i = " + i);
}

JS教程:循环语句

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])
}

JS教程:循环语句

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)

JS教程:循环语句

5、还用一种循环是用for .. of。这个倒是不常见。

var str = "test";


for (var s of str) {
   console.log(s); 
}
       

JS教程:循环语句

  • MySQL错误:Can't connect to(10060)
  • eclipse中新建STM32工程的方法
  • python模块tkinter没有属性tk怎么办
  • 如何实现点击div执行js函数
  • django怎样启动服务器怎样终止服务器
  • 热门搜索
    小女孩发型图片 文件夹怎么隐藏 怎么更改文件格式 xrv怎么样 惊讶的近义词是什么 怎么注册qq号 法令纹深怎么办 二手车交易税怎么算 简单发型 简历的自我评价怎么写