javascript--16-this和字符串

this

  1. 作爲普通函數的調用/自執行 this 在自執行(函數名/變量名())/全局時,this指向window 實際上 並不是指向了window 而是指向null 被解釋成了window 在es5的嚴格模式時候 自執行 this指向undefined
function fn() {
        console.log(this);
    }
    fn();

結果是window

"use strict";//js一般模式嚴格
 function fn() {
        console.log(this);
    }
    fn();

結果時undefined

var t2=function() {
        console.log(this);
    }
   t2();//t2是變量名

this指向window

document.onclick = function () {
        console.log(this);
    }

this指向document document是對象

function t3() {
       function t4() {
           console.log(this);
       }
       t4();
   }
   t3();

this指向window

document.onclick = function () {
      (function () {
          console.log(this);
      })()
  }

this指向window

  1. 作爲對象的方法調用
  2. 作爲對象的方法調用的時候 this是指向 調用的那一瞬間 的調用者 及調用對象 不管函數聲明時,this屬於誰
var bark ="haha";
  var dog = {name:"xiaohua",bark:"wangwang"};
  var show = function () {
      alert(this.bark);
  }
  dog.t = show;
  dog.t();//對象的屬性/方法調用  this指向dog

結果是 汪汪汪

var bark ="haha";
  var dog = {name:"xiaohua",bark:"wangwang"};
  var show = function () {
      alert(this.bark);
  }
  dog.t = show;
  dog.t();
  function goudan() {
      function dachui() {
          console.log(this.bark);
      }
      dachui();
  }
  dog.goudan = goudan;
  dog.goudan();

結果是 haha this指向window

var bark ="haha";
  var dog = {name:"xiaohua",bark:"wangwang"};
  var show = function () {
      alert(this.bark);
  }
 var cat ={bark:"miaomiao"};
  dog.t=show;
  cat.t = dog.t;//dog.t當作值傳過去
  cat.t();//cat調用t

結果是miaomiao

var bark ="haha";
  var dog = {name:"xiaohua",bark:"wangwang"};
  var show = function () {
      alert(this.bark);
  }
 var cat ={bark:"miaomiao"};
 dog.t = show;
 (cat.t=dog.t)();//賦值操作的返回值是等號右邊 是個show函數

結果是haha;

var name ="The window";
  var obj = {
      name:"my name",
      getNameFunc:function () {
          return function () {
              return this.name;
          }
      }
  }
  console.log(obj.getNameFunc()());

this指向window

  1. 作爲構造函數調用時 面向對象解釋
  2. call apply bind

call apply 表示在函數執行時 扭轉this指向

call語法格式: 函數.call(對象,參數1,參數2)
apply語法格式: 函數.call(對象,[參數1,參數2])

function t(num) {
      console.log("我的真實年齡是"+this.age);
      console.log("但我一般告訴他"+this.age-num);
  }
  var hgn = {name:"lily",age:20};
  hgn.t = t;//添加額外屬性 不符合封裝性
  hgn.t(2);

結果是 20 18

function t(num) {
      console.log("我的真實年齡是"+this.age);
      console.log("但我一般告訴他"+this.age-num);
  }
  var hgn = {name:"lily",age:20};
  t.call(hng,2);//t在執行的時候 把自身的this指向了hgn  同時hng並沒有增加t
var obj = {};
 function a(x,y) {
     console.log(x+y);
     console.log(this);
 }
 a.call(obj,3,4);

結果是7 this指向{}

var obj = {};
 function a(x,y) {
     console.log(x+y);
     console.log(this);
 }
 a.apply(obj,[3,4]);
#box{
      width: 100px;
      height: 100px;
      background-color: red;
    }
    #wrap{
      width: 200px;
      height: 200px;
      background-color: blue;
    }
  </style>
</head>
<body>
  <div id="box"></div>
  <div id="wrap"></div>
</body>
box.onclick = function () {
      fn.call(box,200,200)//this指向box
  }
  function fn(x,y) {
      this.style.width = x+"px";
      this.style.height = y+"px";
  }
function a(x,y) {
      console.log(this);
  }
  a.call(100);//指向number
  a.call("haha")//指向string
  a.call(true)//指向boolean
  a.call(null)//被解釋成window
  a.call(this);//window
  a.call([1,2,3])//指向數組

結果是number

bind 在函數定義時 扭轉this指向

function a(x,y) {
      console.log(this);
  }
  document.onclick =a.bind(window);

在點擊的時候 扭轉this指向爲window 與call不同的是 a.call()是已經執行 不需要點擊

document.onclick = function () {
      console.log(this);
  }.bind(window);

點擊時候重新定義 this爲window

var a = function () {
      console.log(this);
  }.bind(document)
  a();

執行時候不是指向window 而是document

  1. 傳參 在表達式中傳參 類似call
  2. 在執行時候傳參  但是失去本身的意義
var a = function (x,y) {
     console.log(x+y);
     console.log(this);
 }.bind(document,10,20);//這樣執行的時候  x y 就一直是10 20  失去意義
 a();

字符集

  1. 字符編碼 計算機 8晶體管 表示開閉狀態 256
  2. a-z +- 0-9 ...表示127個 ASCII
  3. 其他國家 127-255 添加一些補充
  4. 到中國 GB2312 6000多個常見漢字
  5. 產生GBK 拓展很多漢字
  6. GB18030 進一步拓展
  7. 世界範圍 ISO unicode編碼(萬國碼) 包含ASCII前127位和其他國家所有文字的表示
  8. 互聯網的聯通 utf-8 utf-16

字符串方法

  1. var str = "你好";字面量
  2. var str = new String("你好");變爲類數組
  3. var str = String("你好");內建函數

方法

  1. length 可讀不可寫 對象的封裝性
var str = "你好";
  console.log(str.length);

結果是2

var str = "你好";
 str.length = 10;
  console.log(str.length);

結果還是2 是不能修改的

  1. charAt 在那個字符 參數是 索引值
var str = "0anbfj";
console.log(str.charAt(1));

結果是a

  1. concat 表示連接多個字符串 不準用 性能極其低下
var str = "0anbfj";
var str2 = "123";
console.log(str.concat(str2));

結果是0anbfj123

  1. charCodeAt 字符編碼是unicode編碼 參數是索引值
var str = "0anbfj";
console.log(str.charCodeAt(1));

結果是97 ASCII編碼

  1. fromCharCode 來自某一個字符編碼
console.log(String.fromCharCode(97));

結果是a

  1. toUpperCase()轉大寫
var str = "anjsdf";
console.log(str.toUpperCase());

結果是ANJSDF

  1. toLowerCase()轉小寫
var str = "ANjsdf";
console.log(str.toLowerCase());

結果是anjsdf

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章