js笔记

function myFunction(x, y) {
    y = y || 0;
    return x * y;
}
document.getElementById("demo").innerHTML = myFunction(4);

//0

 

自调用函数

(function () {
    document.getElementById("demo").innerHTML = "Hello! 我是自己调用的";
})();

//添加括号,来说明它是一个函数表达式
//表达式后面紧跟 () ,则会自动调用

匿名函数

var x = function (a, b) {return a * b};  //以分号结尾,因为它是一个执行语句
document.getElementById("demo").innerHTML = x(4, 3);

 

function myFunction(a,b){
    return a*b;
}                                        //函数声明不是一个执行语句,所以不以分号结束
document.getElementById("demo").innerHTML=myFunction(4,3);

分号是用来分隔执行的JavaScript语句。

json

{"sites":[         //json 数组
    {"name":"Runoob", "url":"www.runoob.com"},   //json 对象 
    {"name":"Google", "url":"www.google.com"},
    {"name":"Taobao", "url":"www.taobao.com"}
]}
typeof null      //object,
typeof undefined       //undefined

 

undefined 是一个没有设置值的变量

var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
var person = undefined;
document.getElementById("demo").innerHTML =
	person + "<br>" + typeof person;

//var person = undefined;   设置 = undefined 来清空对象
//person 的值是:undefined
//person 的类型是:undefined

  

 

for/in循环 遍历对象

function myFunction(){
    var x;
    var txt="";
    var person={fname:"Bill",lname:"Gates",age:56}; 
    for (x in person){
        txt=txt + person[x];
    }
    document.getElementById("demo").innerHTML=txt;
}
//BillGates56

parseInt() //解析一个字符串,并返回一个整数 (怕字儿,所以数儿)

document.write(parseInt("10") + "<br>");               //10
document.write(parseInt("10.33") + "<br>");          //10
document.write(parseInt("34 45 66") + "<br>");     //34
document.write(parseInt(" 60 ") + "<br>");            //60
document.write(parseInt("40 years") + "<br>");     //40
document.write(parseInt("He was 40") + "<br>");   //NaN

parseFloat()  //解析一个字符串,并返回一个浮点数

原文地址:https://www.cnblogs.com/qq254980080/p/8357976.html