Web front-end --JavaScript notes

js

type of data

The string can be '' or ''

Operators

  • == Only relatively content
  • === Comparison of content, but also more type is the same, only two are true is true
  • !==And above ===contrary
var a=123;
var b="123";
//a==b 结果为true
//a===b 结果为false
//a!==b 结果为true

variable

js the variables are global, local variables the same name will also change the original data

var i=2;
for(var i=0;i<5;i++){
    console.log('hello');
}
console.log(i);//这里的i等于4

let Keywords local variable, does not modify the global variable

var i=2;
for(let i=0;i<5;i++){
    console.log('hello');
}
console.log(i);//这里的i等于2

method

js compiled from top to bottom, however, if the method is in the back, he can perform the method, the compiler even if no method of

//console.log(hello); 打印函数内容
//console.log(hello()); 打印函数内容,之后执行函数
hello();
function hello(){
    console.log("hello world!");
}

Method returns without return type defined in the foregoing method

function print(){
    return "";
}

other

  • querrySelector

    querrySelector is in jQuery method
    querrySelector ( "# mydiv p") getElementById compared with other methods, more flexible, allowing a hierarchical relationship
    • querrySelector ( "# mydiv") id selector
    • querrySelector ( "div") tag selector (only get the first div tag)
    • querrySelector ( ". mydiv") class selector
  • innerHtml If there is text html, which can recognize text html tags
  • innerText Only into the text, which does not recognize the tag

The body tag onload equivalent windows.onload

  • es6 (JavaScript1.6) string concatenation
//es6中,year和month是变量 换行直接换,不需要“+”
var str = `${year}年${month}月`;
//会保留空格以及换行
var str = `${year}年
            ${month}月`
  • Analyzing element is empty es6
var e = document.getElementById("h");
//e存在为true,不存在则为false
if(e){
    
}
  • es6 array traversal
var array1 = ["a","b","c"];
array1.foreach(function(element){
    console.log(element);
});

Guess you like

Origin www.cnblogs.com/kexing/p/11105730.html