1. 函数实例化原理

1.1 构造函数中的this

  • this指向的不是构造函数本身
  • 当构造函数实例化的时候,AO生成 就已经生成了this
  • new实例化了对象之后,this指向的是这个实例化的对象,在没有实例化之前,指向的window ```javascript // 在没有实例化之前,指向的是window,new实例化之后,this指向的是这个实例化的对象 function Car(color, brand){ this.color = color; this.brand = brand; console.log(this); }

Car(); var c1 = new Car(‘red’, ‘mazda’); // 第一次输出 window // 第二次输出 Car{}

// 上述实例化理解 function Car(color, brand){ this.color = color; this.brand = brand; // var this ={ // color: color, // brand: brand, // } // 隐式增加了return this }

// 不用new 直接指向函数 function Car(color, brand){ var obj = {} obj.color = color; obj.brand = brand;

return obj }

var car = Car(‘red’, ‘audi’); console.log(car);

  1. <a name="eLJoq"></a>
  2. ## 1.2 构造函数中的return
  3. - number, string, undefined, boolean, null等原始值,会被忽略,结果还是return this
  4. - [], {}, function(){},等引用值,会改变返回引用值
  5. ```javascript
  6. function Car(){
  7. return null // undefined, string boolean, number
  8. }
  9. var car = new Car();
  10. console.log(car); // Car {}
  11. function Car(color){
  12. this.color = color;
  13. return [] // {} function(){}
  14. }
  15. var car = new Car();
  16. console.log(car); // [], {}, function(){}
  17. console.log(car.color); // undefined

2. 包装类

2.1 new Number 转换为数字对象

  1. var a = 1;
  2. console.log(a); // 1 原始值
  3. var b = new Number(a);// 数子对象 => 实例化对象后的数字
  4. console.log(b); // Number{1}
  5. b.len = 1;
  6. b.add = function(){
  7. console.log(1);
  8. }
  9. console.log(b);// Number {1, len: 1, add: f()}
  10. var d = b + 1;
  11. console.log(d);// 2 此时的运算使得b又变成了一个原始值
  12. console.log(b);// Number {1, len: 1, add: f()} 参与运算之后,又成了数字对象

2.2 new string 转换为字符串对象

  1. var a = 'abc';
  2. console.log(a); // abc
  3. var aa = new String('abc');
  4. aa.name = 'aa';
  5. console.log(aa);// {'abc', name:'abc'}
  6. var bb = aa + 'bcd';
  7. console.log(bb);// 'abcbcd'
  8. console.log(aa); // {'abc', name:'abc'}

2.3 JS包装类过程

2.3.1 原始值并没有自己的属性和方法

给原始值添加属性和方法, 系统会包装类进行添加, 但是没有提供变量用于保存, 系统会默认delete

  1. // 原始值并没有自己方法和属性
  2. var a = 123;
  3. a.len = 3;
  4. // a是原始值,不能添加属性
  5. // new Number(a).len = 3;
  6. // delete new Number()
  7. cosnole.log(a.len);
  1. var str = 'abc';
  2. // new String(str).length -> str经过了包装类
  3. console.log(str.length);// 3

2.3.2 数组截断

数组可以被length 进行截断

  1. // 数组的截断
  2. var arr = [1, 3, 4, 5, 6];
  3. arr.length = 3;
  4. console.log(arr); // [1, 3 ,4]
  5. arr.length = 6;
  6. // arr -> [1, 3, 4, 5, 6, empty]
  7. // arr[6] => undefined
  8. // 字符串的截断?
  9. var str = 'abc';
  10. str.length = 1;
  11. // new String(str).length = 1 -> delete
  12. console.log(str);// abc
  13. // 再次new String(str).length
  14. concole.log(str.length);// 3

3.案例

案例1

  1. var name1 = 'languiji';
  2. name1 += 10; // name1 => 'languiji10'
  3. var type = typeof(name1);// 'string'
  4. if(type.length === 6){
  5. type.text = 'string';
  6. // new String(type).text = 'string'
  7. // delete
  8. }
  9. console.log(type.text); // undefined

案例2

  1. function Test(a, b, c){
  2. var d = 1;
  3. this.a = a;
  4. this.b = b;
  5. this.c = c;
  6. function f(){
  7. d++;
  8. console.log(d);
  9. }
  10. this.g = f;
  11. }
  12. var t1 = new Test();
  13. t1.g(); // 2
  14. t1.g(); // 3
  15. var t2 = new Test();
  16. t2.g(); // 2

案例3

  1. var x = 1,
  2. y = z = 0;
  3. function add(n){
  4. return n = n + 1;
  5. }
  6. y = add(x);
  7. function add(n){
  8. return n = n + 3;
  9. }
  10. z = add(x);
  11. console.log(x, y, z); // 1 4 4

案例4

  1. // 函数1
  2. function foo1(){
  3. console.log(arguments);
  4. return x;
  5. }
  6. foo1(1, 2, 3, 4, 5);
  7. // 函数2
  8. function foo2(x){
  9. console.log(arguments);
  10. return x
  11. }(1, 2, 3, 4 ,5)
  12. // 函数3
  13. (function foo3(x){
  14. console.log(arguments);
  15. return x
  16. })(1, 2, 3, 4, 5)
  17. 以上那个函数可以输出 1, 2, 3, 4, 5
  18. 1.3可以
  19. 2 => (1, 2, 3, 4, 5)被当做表达式,不会被执行

案例5

  1. // 当非严格模式下中的函数没有包含 *剩余参数,默认参数,解构赋值,那么arguments对象中的值会跟踪参数的值
  2. function b(x, y, a){
  3. // a = 10;
  4. // console.log(arguments[2]); // 10
  5. arguments[2] = 10;
  6. console.log(a); // 10
  7. }
  8. b(1, 2, 3);
  9. 严格模式下不会跟踪参数的值
  10. 'use strict'
  11. function b(x, y, a){
  12. arguments[2] = 10;
  13. console.log(a); // 3
  14. }
  15. b(1, 2, 3);

4.练习

写一个函数,接受任意一个字符串,算出这个字符串的总字节数

  1. // ASCII 码 表1 0- 127, 表2 128-255
  2. // 一个字节
  3. // UNICODE码 -> 涵盖ASCII码 2个字节
  4. function bytes(str){
  5. var allBytes = 0;
  6. for(var i = 0; i < str.length; i++){
  7. var code = str.charCodeAt(i);
  8. if(code <= 256){
  9. allBytes += 1;
  10. }else{
  11. allBytes += 2;
  12. }
  13. }
  14. console.log(allBytes);
  15. }
  16. bytes('你好,中国! hello!');
  17. function getBytes(str){
  18. var Bytes = str.length;
  19. for(var i = 0; i < str.length; i++){
  20. var codeIndex = str.charCodeAt(i);
  21. if(codeIndex > 255){
  22. Bytes++;
  23. }
  24. }
  25. return Bytes;
  26. }