Skip to content

[js] 第94天 用js写出死循环的方法有哪些? #956

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

第94天 用js写出死循环的方法有哪些?

Activity

dondonZh

dondonZh commented on Jul 19, 2019

@dondonZh

while(true){
}

for(let i = 0;i<0;i++){
}

NicholasBaiYa

NicholasBaiYa commented on Jul 19, 2019

@NicholasBaiYa

循环/递归不设置结束条件。
function fn(n){
fn(n+1)
}

ghost

ghost commented on Jul 19, 2019

@ghost

非常规解法

const infiniteIterable = {
    [Symbol.iterator]() {
        const thisRef = this
        return {
            value: true, // or any other value
            next() {
                return thisRef[Symbol.iterator]()
            }
        }
    }
}
for (let _ of infiniteIterable) {
    // ...
}
fireairforce

fireairforce commented on Jul 19, 2019

@fireairforce

while(1){
console.log('%%%%');
}

xxf1996

xxf1996 commented on Jul 19, 2019

@xxf1996
  1. while
while (true) {

}
  1. for
for (;;) {

}
nowherebutup

nowherebutup commented on Jul 19, 2019

@nowherebutup

1.递归不限制条件
2.while(true){ }

Konata9

Konata9 commented on Aug 20, 2019

@Konata9

循环如果能结束就不是死循环,即设置一个永远达不到的结束条件就能造成死循环。

while (true) {}

for (let i = 0; i > 0; i++) {}

// 这个等价于 while
for (;;) {}

let i = 0;
do {
  i++;
} while (i > 0);

// 不设置结束条件
function fn(a) {
  console.log(a);
  fn(a);
}
kruzabc

kruzabc commented on Jan 8, 2020

@kruzabc

递归不优化做无限循环就容易爆栈,从而报错。

smile-2008

smile-2008 commented on Jul 1, 2021

@smile-2008

循环如果能结束就不是死循环,即设置一个永远达不到的结束条件就能造成死循环。

while (true) {}

for (let i = 0; i > 0; i++) {}

// 这个等价于 while
for (;;) {}

let i = 0;
do {
  i++;
} while (i > 0);

// 不设置结束条件
function fn(a) {
  console.log(a);
  fn(a);
}
xiaoqiangz

xiaoqiangz commented on Jun 23, 2022

@xiaoqiangz

1 递归不限制条件
2 while(true){}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @smile-2008@haizhilin2013@Konata9@xiaoqiangz@xxf1996

        Issue actions

          [js] 第94天 用js写出死循环的方法有哪些? · Issue #956 · haizlin/fe-interview