随笔

Javascript ES5 Special Features

声明提权

变量声明并赋值时,声明会提到作用域顶部,赋值留在原地 另外,函数声明也会提前

function a(){
    console.log("top");
    var b = 1;
    console.log(b);
}
// 转成伪代码
function a(){
    var b;
    console.log("top");
    b = 1;
    console.log(b);
}

闭包

咋说呢,就是把它围起来,不让里面的东西暴露出来

function print(type){
    var prefix = "Hello ";
    function printHow(name){
        console.log(name + " ?");
    }
    function printWow(name){
        console.log(prefix + name + " !");
    }
    if(type && type == 1){
        return printWow 
    }else{
        return printHow
    }
}
(function(){
    var resu = print(1);
    resu("lilonghe");

    var resuH = print();
    resuH("lilonghe");
}());

OOP

Javascript中,函数都会有prototype属性,该属性指向原型对象

想起了C#里面的基础对象还有判断是否继承的instanceof
据说ES6class也是根据这个演化而来

创建

// 同 C#
function Print(size){
    this.name = 1;
    this.size = size;
}
var obj=new Print("a4");

继承

function PrintA5(){
    this.name = "WOW";
}
PrintA5.prototype = new Print("A5");
console.log(PrintA5.prototype.size);

本文链接:https://note.lilonghe.net/post/javascript-es5-special-features.html

-- EOF --