javascript基礎(第一天)

來源:互聯網
上載者:User

標籤:

前面一句話: 挖掘知識的邊界,摳的越細,深度和廣度才越高,才越牛.

js的效能已經達到java的五分之一

(1) == ===的區別??

      null == undefined //true
      ‘‘ == false //true
      總結 ==不靠譜, 只用===

(2) 不使用continue;

      效率低,出處:javascript語言精粹111頁


(2) var a = {};

      a.b=‘xx‘;
      a[‘delete‘] = ‘yy‘; ///關鍵詞也無所謂了


(3) delete 刪屬性
      全域變數和局部變數刪除不掉???

      var aaaa = 123;
      delete aaaa;
      console.log(aaaa); //123

      window.aaaa = 123;
      delete window.aaaa;
      console.log(window.aaaa); //undefined

      function a(){
            var a= 123;
            delete a;
            console.log(a); //123
      }a();

     設為a=null;祈禱上天,等待記憶體回收機制回收.

     註:當使用var聲明一個變數時,建立的這個屬性是不可配置的,也就是說無法通過delete運算子刪除

     var name=1    ->不可刪除

     sex=”girl“         ->可刪除

     this.age=22    ->可刪除



(4) typeof instanceof

      typeof 2 === ‘number‘ //true
      typeof new Number(2) === ‘number‘ //false
      typeof ‘2‘ === ‘string‘ //true
      typeof new String(‘2‘) === ‘string‘ //false
      typeof true === ‘boolean‘ //true
      typeof new Boolean(true) === ‘boolean‘ //false
      typeof [] === ‘object‘ //true
      var a = function(){}; typeof a === ‘function‘; //true

      [] instanceof Object //true
      2 instanceof Number //false
      new Number(2) instanceof Number //true

      var a = function(){}
      var b = function(){};
      a.prototype = new b();
      new a() instanceof b; //true

      // typeof 能判斷 基本類型 和 function
      // instanceof 能判斷 自訂對象

      //擴充 Object.prototype.toString.call([]) === ‘[object Array]‘; 這樣是最棒的判斷

(4) if語句
      if( xxxx ) ? //6種情況為false

      // null undefined NaN 0 ‘‘ false

(4) for in 
      //遍曆對象

      //遍曆鬆散的數組

      var a = new Array(10); a[2]=10; a[5]=10; for(var i in a){ console.log(i); }

       效能,確實最佳化了

      var a = new Array(10000000);
      a[2]=10;
      a[5]=10; 
      var time1 = new Date().getTime();
      for(var i in a ){ 
            Math.random()* Math.random()* Math.random() * Math.random();
      }
      var time2 = new Date().getTime();
      for(var i=0; i<a.length; a++){ 
            Math.random()* Math.random()* Math.random() * Math.random();
      }
      var time3 = new Date().getTime();
      console.log(time2-time1); //~100ms
      console.log(time3-time2); //~200ms

(5) 函數字面量 與 函式宣告

      var a = 2;
      function a(){}
      console.log(typeof a); //function ? number ?

 

      總結:
      變數的聲明被提前到範圍頂部,賦值保留在原地;
      聲明式函數整個“被提前”;
      函數運算式只有變數“被提前”了,函數沒有“被提前”;
      函數的聲明比變數的聲明具有高的優先順序。
      賦值會根據前後順序進行覆蓋!

      var a = function(){
            console.log(‘aaa‘);
      }
      function a(){
            console.log(‘bbb‘);
      }
      //var b = function b(){} === function b(){}
      a();

(6) 函數傳回值
      var a = function(b,c){
            console.log(b+‘,‘+c);
      }
      console.log( a() );

(7) this指向
      function a(){

            var that = self = this;

            this.v = ‘aaa‘;
            function b(){
                  console.log(this);
                  console.log(this.v);
            }
            b();
      }
      new a();

(8) arguments
      function a(a,b,c,d){
            //var args = [].slice.call(arguments);
      }

(9) apply call
      function a(){
            console.log(this.x+this.y);
      }
      a.call({x:100,y:100});

      function C(){
            this.m = function(){
                  console.log(this.x+this.y); 
            }
      }
      var c = new C();
      c.m.call({x:100,y:100});

      //這個就是this的重新導向,原諒我吧,沒法解釋的很準確和淺顯易懂,只能多練習

(10) try catch throw 關鍵邏輯加上就好
      var a = ‘aaa‘;
      try{
            a = xxxx(); //不存在的方法
      }catch(e){
            throw(new Error(‘error‘));
            a = 0;
      }
      console.log("go go go"); //這裡走嗎???

(11) 閉包
      for(var i=0; i<2; i++){
      }
      console.log(i);

      function a(){
            //只有方法體的才有範圍
      }

      

      var a;
      function c(){
      var v = ‘vvv‘;
      function b(){
            return {v:v};
      };
      a = b();
      }
      c();
      console.log(a.v); //方法體是範圍, 這個大家都知道了, 內部的方法體需要給個口子出來,讓外面可以訪問到.

(12) 回調

      function a(callback){
            callback();
      }
      a(function(){
            console.log(‘aaa‘);
      });

(13) 自執行函數, 也叫自調函數

      (function(a){
            console.log(a);
      })(‘aaa‘);

(14) 正則
      var regexp = / /img
            //i   大小寫
            //m 換行
            //g  檢索到第一個匹配不停止,檢索全部

(15) prototype
      //prototype是什麼?
      function a(){
            this.v=‘vvv‘;
      }
      console.log(a.prototype); //當前對象,函數也是對象,
      console.log(a.prototype.constructor); //當前方法
      console.log(a.prototype.constructor.__proto__ === a.__proto__); //true
      //prototype是函數的內建屬性,__proto__是對象的內建屬性,都是尋找原型鏈的

      //prototype使用情境?
      var b = {};
      //function b(){};
      //var b = function(){}
      b.prototype.bbb=‘bbb‘;
      console.log(b.bbb);
      //console.log(new b().bbb); //構造器

      //prototype咋玩? (下面的都是不好的用法)
      function a(){};
      a.prototype = function(){ console.log("aaa") };
      console.log( new a.prototype() );

      // function a(){}
      // function b(){}
      // a.prototype = {aaa:‘aaa‘};
      // b.prototype = a.prototype;
      // var aa1 = new a(),
      // aa2 = new a(),
      // bb1 = new b();
      // aa1.prototype.aaa=‘xxx‘;
      // console.log( aa2.aaa );
      // console.log( bb1.aaa );

      // function a(){}
      // function b(){ this.bbb=‘bbb‘};
      // a.prototype = new b();
      // console.log(new a().bbb );

      //-----4 其實是這樣的
      Object.create();


最後一句話: !!!只有最佳技術實踐才能救命!!!

javascript基礎(第一天)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.