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

猜你喜欢

转载自blog.csdn.net/lxhby520/article/details/80514637