js基礎知識點收集

來源:互聯網
上載者:User

標籤:全域   調用   函數   function   fine   class   1.5   span   參考型別   

js基礎知識點收集js常用基本類型
function show(x) {        console.log(typeof(x));    // undefined        console.log(typeof(10));   // number        console.log(typeof(‘abc‘)); // string        console.log(typeof(true));  // boolean        console.log(typeof([]));  // object        console.log(typeof(function () {        }));  //function        console.log(typeof([1, ‘a‘, true]));  //object        console.log(typeof ({a: 10, b: 20}));  //object        console.log(typeof (null));  //object        console.log(typeof (new Number(10)));  //object    }    show();

實值型別:undefined, number, string, boolean
參考型別:函數、數組、對象、null(一切參考型別都是對象,對象是屬性的集合)

js原型及原型鏈
function Fn() { }        Fn.prototype.name = ‘王福朋‘;        Fn.prototype.getYear = function () {            return 1988;        };        var fn = new Fn();        console.log(fn.name);        console.log(fn.getYear());

fn是Fnnew出來的對象,fn.proto === Fn.prototype

js原型鏈
 function Foo() {    }    var f1 = new Foo();        //f1.a = 10;    Foo.prototype.a = 100;    Foo.prototype.b = 200;    console.log(f1.a);//100    console.log(f1.b);//200

f1賦值的時候有

    console.log(f1.a);//10    console.log(f1.b);//200

原型鏈:訪問一個對象的屬性時,先在基本屬性中尋找,如果沒有,再沿著__proto__這條鏈向上找,這就是原型鏈

js原型鏈繼承
      A = {        a: function () {            console.log(‘a‘);        }    }    B= {        b: function () {            console.log(‘b‘);        }    }    A.a();//a    B.b();//b    Object.setPrototypeOf(A,B);//把B的原型給A    A.b();//b
js上下文

提到js的上下文先說下this關鍵字
在函數中this到底取何值,是在函數真正被調用執行的時候確定的,函數定義的時候確定不了

情況1:建構函式

   function A() {        this.a = ‘a‘;        this.hello = function () {            console.log(this.a);//a        }        console.log(this);//{a: "a"}    }    var a = new A();    a.hello();

如果函數作為建構函式用,那麼其中的this就代表它即將new出來的對象。

情況2:函數作為對象的一個屬性
console.log(this.a);//a

情況3:函數用call或者apply調用

  var B = {        b: ‘b‘    }    var A = function () {        console.log(this.b);    }    A.call(B);//b

當一個函數被call和apply調用時,this的值就取傳入的對象的值。

情況4:函全域 & 調用普通函數
在全域環境下和普通函數調用的時候,都是window

js基礎知識點收集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.