读<了不起的Node.js>-02.JavaScript基本介绍

2018-08-04  本文已影响0人  在路上的海贼

JavaScript

介绍

类型

//基本类型
let a = 5;
let b = a;
b = 6;

console.log(a);
console.log(b);

console.log('``````````````````````````````````````');
//复杂类型
let x = ['hello', 'world'];
let y = x;
console.log(y[0]);
y[0] = 'bye';
console.log(x[0]);
console.log(y[0]);

类型的困惑

let a = 'woot';
let b = new String('woot');

console.log(typeof a);
console.log(typeof b);

//检查这个值的诗句类型是否属于 string
console.log(a instanceof String);
console.log(b instanceof String);
//判断变量值是否相等
console.log(a == b);
//判断地址值是否相等
console.log(a === b);

let a = 0;
if (a) {
    //这里永远执行不到
}
a==false //true
a=== false // false

函数

let a = function () { };
console.log(a);//将函数作为参数传递的
let a = function () { };

console.log(a);//将函数作为参数传递的


let b = function b() {
    if ('function' == typeof b) {//书中数这个结果是 true 但是我没有打印出任何结果
        console.log('true');
    }else{
        console.log('false');
    }
};

b(); //原来是我没有调用..
function c(){
    if (window ==this){
        console.log("true");
    } else{
        console.log("false");
    }
}

c();//这样调用会报错 window is not defined
function a (b,c){
    b == 'first';//true
    c == 'second';//ture
}

a.call({a:'b'},'first','second');
a.apply({a:'b'},['first','seccond']);

函数的参数数量

闭包

//这里用var和用let 的结果会不一样,var 的作用域更大 且支持重复命名
//let是不支持重复命名的
var a = 5;
function woot() {
    a ===5 ;//true

    var a = 6;
    function test() {
        a === 6; //true
    }

    test();
};

woot();

let a = 3;

(function () {
    let a = 5;
})();

a ==3;//true

function Animal ( )  {}
Animal.prototype.eat = function (food)  {
    //eat method
}
function Animal (name) {
    this.name = name;
}

Animal.prototype.getName(){
    return this.name;//这里有问题 我用的idea
}

let animal = new Animal('tobi');
a.getName() == 'tobi'; //true

继承

try{} catch{}

上一篇 下一篇

猜你喜欢

热点阅读