# JS 字符串、箭头函数、生成器函数

2019-07-25  本文已影响0人  北疆小兵

JS Chapter 1

Mac 下的Chrome 按什么快捷键调出页面调试工具

开发者工具:option+command+i
javascript控制台:option+command+j
或者按option+command+c也可以打开

字符串

function show(){
    console.log(1);
}
show``;
let f = 5;
let g=  10;
console.log(`${f + g}`)
[1,2,3,4,5].find(function(x){
            console.log(x);
        });

函数


functionName = x => x;
        console.log(functionName(2));

        var functionName2 = function(x ){
            return x;
        }
        console.log(functionName2(2));
        

其中functionName 代表函数名 ,第一个x党代表参数,第二个参数代表返回值

show = () =>{
    console.log(1);
}
show``;

show函数没有传递参数,  可以像上面这么写

[1,2,3].find(function(x){
            console.log(`find1: ${x}`);
        });

        [1,2,3,4].find(x =>{
            console.log(`find2: ${x}`);
        });

        [1,2,3,4].find((x,y,z) =>{
            console.log(`find4: ${x},${y},${z}`);
        });
 document.onclick=function({     
   document.body.style.background='red';
}

document['onclick'] = () =>{            
document.body.style.background='red;
}

(function(){
        alert(1);
})()


let x = 10;
((x) =>{
    console.log(x);
})(x)
        
function show2(x=5){
            console.log(x);
        }
        show2();
        show2(3);

function yanzhan(y, ...x){
    console.log(x);
}
yanzhan(1,2,3,4);

for(var i = 0; i <5;i++){

        }
        console.log(i); //i 会泄漏,输出5,相当于var是全局的

for(let j = 0; j <5;j++){

        }
        console.log(j);//Uncaught ReferenceError: j is not defined
    at


     const a = 10;
        a = 20;
        console.log(a); //会报错 Uncaught TypeError: Assignment to constant variable

let div = `<div></div>`;
        document.write(div.repeat(5));
var c = [];
        var d = document.getElementsByTagName('div');
    for(var i = 0;i < d.length; i++){
            c.push(d[i]);
        }
        console.log(c);

querySelectorAll写法

console.log([...document.querySelectorAll('div')]);

生成器函数

function* show(){
            yield () =>{
                console.log(1);
            }
            yield function(){
                //alert(2)
                console.log(2);
            }
        }
        //console.log(show())
        var k = show();
        k.next().value();
        k.next().value();


上一篇 下一篇

猜你喜欢

热点阅读