问题解决

NaN

  1. 0/0 => NaN
  2. typeof(0) => number
  3. typeof(NaN) => number
  4. typeof(0/0) => NaN => number

typeof ()返回的是字符串

  1. console.log(typeof (a)); //underfined(string)
  2. console.log(typeof ('undefined')); //string
  3. console.log(typeof (undefined)); //underfined(string)
  1. //'undefined' -> true
  2. //undefined -> false
  3. //typeof (a) -> typeof (undefined) -> 'undefined' -> true
  4. //(-true) -> -1
  5. //(+undefined) -> Number(undefined) -> NaN
  6. //(-1 + NaN + '') -> 'NaN' -> true
  7. //true && true -> true -> console.log('通过了')
  8. if (typeof (a) && (-true) + (+undefined) + '') {
  9. console.log('通过了');
  10. } else {
  11. console.log('没通过');
  12. }

实例化之前的prototype和实例化的prototype是有区别的

  1. Car.prototype.name = 'Benz';
  2. function Car() {}
  3. var car = new Car();
  4. //还没实例化
  5. Car.prototype = {
  6. name: 'Mazda'
  7. }
  8. console.log(car.name); //Benz
  9. //实例化以后
  10. function Car() {
  11. var this = {
  12. __proto__: Car.prototype = {
  13. name: 'Benz'
  14. }
  15. }
  16. }

思考:子代赋值是否影响被继承的原型?子代赋值还是父代被赋值?

  1. function Teacher() {
  2. this.mSkill = 'JS/JQ';
  3. this.success = {
  4. alibaba: '28',
  5. tencent: '30'
  6. }
  7. }
  8. var teacher = new Teacher();
  9. Student.prototype = teacher;
  10. function Student() {
  11. this.pSkill = 'HTML/CSS'
  12. }
  13. var student = new Student();
  14. student.success.baidu = '100';
  15. console.log(teacher);
  16. //Professor {mSkill: "JS/JQ", success: {alibaba: "28", tencent: "30", baidu: "100"}}
  17. console.log(student);
  18. //Student {pSkill: "HTML/CSS"}

结果看出没有赋值到实例对象里而是赋值到实例对象的原型上

  1. //alibaba
  2. function b(x, y, a) {
  3. // arguments[2] = 10;
  4. // alert(a); //10
  5. //形参和实参具有映射关系,谁被改都会更改
  6. a = 10;
  7. alert(arguments[2]); //10
  8. }
  9. b(1, 2, 3);
  10. //b(1, 2, 3) => b(1, 2, 10)
  1. //undefined > 0 -> false
  2. //undefined < 0 -> false
  3. //undefined = 0 -> false
  4. //null > 0 -> false
  5. //null < 0 -> false
  6. //null = 0 -> false
  7. //undefined == null -> true
  8. //undefined === null -> false
  9. //isNaN('100') -> false
  10. //isNaN('abc') -> true
  11. //var num = Number('100') -> isNaN(num) -> false
  12. //parseInt('123a') -> 123
  13. //parseInt('1a') -> 1
  14. console.log(undefined == null); //true
  15. console.log(undefined === null); //false
  16. console.log(isNaN('100')); //false
  17. console.log(parseInt('1a') == 1); //true
  1. var a = 5;
  2. function test() {
  3. a = 0;
  4. console.log(a);
  5. console.log(this.a);
  6. var a;
  7. console.log(a);
  8. }
  9. test(); //0 5 0
  10. new test(); //0 undefined 0
  1. //alibaba
  2. var name = '222';
  3. var a = {
  4. name: '111',
  5. say: function () {
  6. console.log(this.name);
  7. }
  8. }
  9. var fun = a.say;
  10. /**
  11. * var fun = function(){
  12. * console.log(this.name); //this -> window
  13. * }
  14. */
  15. fun(); //222
  16. a.say(); //111 this-> a
  17. var b = {
  18. name: '333',
  19. say: function (fun) {
  20. fun();
  21. /**
  22. * function(){
  23. * console.log(this.name); //this -> window
  24. * }()
  25. */
  26. }
  27. }
  28. b.say(a.say); //222
  29. b.say = a.say;
  30. b.say(); //333
  31. //b.say = function(){
  32. // console.log(this.name); this -> b
  33. //}
  1. var bar = {
  2. a: '1'
  3. };
  4. function test() {
  5. bar.a = 'a'; //这里覆盖全局的值
  6. Object.prototype.b = 'b';
  7. return function inner() {
  8. console.log(bar.a); //'a'
  9. console.log(bar.b); //'b'
  10. }
  11. }
  12. test()();
  13. //此处test()() -> var test = test(); -> test();
  1. //总结知识点
  2. //1.函数也是一个对象
  3. //2.变量提升
  4. //3.函数内部没有var就会提升到全局
  5. //4.构造函数实例化内部没有的属性和方法会去访问原型
  6. //5.预编译过程:变量,函数声明,函数表达式之间的赋值先后
  7. //6.优先级:执行符号()/new + 执行符号() > .调用 > new
  8. //7.new + 数字结果 没有输出
  9. /**
  10. * GO = {
  11. * getName: undefined
  12. * -> function getName(){console.log(5)}(提升)
  13. * -> function(){console.log(4)}(被赋值)
  14. * -> function(){console.log(1)}(被没有var声明的getName函数覆盖)
  15. * }
  16. *
  17. */
  18. //构造函数
  19. //3.Foo().getName()
  20. //
  21. function Foo() {
  22. //这里AO需要调用Foo()才能进来
  23. //这里getName没有var声明会直接提升到全局
  24. getName = function(){
  25. console.log(1);
  26. }
  27. return this;
  28. }
  29. //Foo函数底下声明getName属性,值为函数
  30. //1.Foo.getName();
  31. //分析Foo.getName() 只是借用Foo对象里面的方法 如 .length 跟Foo函数本身没有任何关系 JS中函数是一种特殊的对象
  32. Foo.getName = function () {
  33. console.log(2);
  34. }
  35. //Foo函数底下原型上声明getName属性,值为函数
  36. //(这里需要实例化构造函数Foo()才能访问)
  37. Foo.prototype.getName = function () {
  38. console.log(3);
  39. }
  40. //全局声明变量getName接收匿名函数
  41. //2.getName()
  42. //这里预编译时保持变量getName
  43. var getName = function () {
  44. console.log(4);
  45. }
  46. //函数声明
  47. function getName() {
  48. console.log(5);
  49. }
  50. Foo.getName(); //2
  51. //直接访问Foo对象下的getName方法
  52. getName(); //4
  53. //getName函数提升然后被全局变量getName接收赋值覆盖
  54. Foo().getName(); //1
  55. //Foo()执行,内部没有var声明的getName函数提升到全局并覆盖之前的值所以打印1
  56. getName(); //1
  57. //这里执行的是全局的getName 打印1
  58. new Foo.getName(); //2
  59. //这里.运算 是比new的优先级高的
  60. //先执行Foo.getName() 打印2 然后 new 数字2 是没意义的 没有输出结果
  61. new Foo().getName(); //3
  62. //规律这里执行符号()的优先级比 .大 且执行的同时包含了new
  63. //实例化的时候构造函数Foo内部没有this.getName属性,但是原型上有 所以打印原型上的3
  64. new new Foo().getName(); //3
  65. //情况跟new Foo().getName()一样,然后再new 数字3 没意义没输出
  66. //我写的答案: 1 4 1 5 2 1 1
  67. //答案:2 4 1 1 2 3 3
  1. //封装myTypeof方法
  2. //Object.prototype.toString.call();
  3. //原始值:
  4. //typeof('123') -> 'string';
  5. //toString.call('123') -> '[object String]'
  6. //typeof(123) -> 'number';
  7. //toString.call(123) -> '[object Number]'
  8. //typeof(null) -> 'object';
  9. //toString.call(null) -> '[object Null]'
  10. //typeof(NaN) -> 'number';
  11. //toString.call(NaN) -> '[object Number]'
  12. //typeof(undefined) -> 'undefined';
  13. //toString.call(undefined) -> '[object Undefined]'
  14. //typeof(true) -> 'boolean';
  15. //toString.call(true) -> '[object Boolean]'
  16. //引用值
  17. //typeof({}) -> 'object';
  18. //toString.call({}) -> '[object Object]'
  19. //typeof([]) -> 'object';
  20. //toString.call([]) -> '[object Array]'
  21. //typeof(function(){}) -> 'function';
  22. //toString.call(function(){}) -> '[object Function]'
  23. //包装类
  24. //typeof(new Number(1)) -> 'object';
  25. //toString.call(new Number(1)) -> '[object Number]'
  26. //typeof(new String('1')) -> 'object';
  27. //toString.call(new String('123')) -> '[object String]'
  28. //typeof(new Boolean(true)) -> 'object';
  29. //toString.call(new Boolean(true)) -> '[object Boolean]'
  30. function myTypeof(val) {
  31. var toStr = Object.prototype.toString,
  32. type=typeof(val),
  33. res = toStr.call(val);
  34. //包装类和 引用值 null {} []
  35. if (type === 'object') {
  36. switch (res) {
  37. case '[object Null]':
  38. type = 'null';
  39. break;
  40. case '[object Object]':
  41. type = 'object';
  42. break;
  43. case '[object Array]':
  44. type = 'array';
  45. break;
  46. case '[object Number]':
  47. type = 'object number';
  48. break;
  49. case '[object String]':
  50. type = 'object string';
  51. break;
  52. case '[object Boolean]':
  53. type = 'object boolean';
  54. break;
  55. }
  56. }
  57. return type;
  58. }
  59. //原始值
  60. console.log(myTypeof('123')); //'string'
  61. console.log(myTypeof(123)); //'number'
  62. console.log(myTypeof(null)); //'null'
  63. console.log(myTypeof(NaN)); //'number'
  64. console.log(myTypeof(undefined)); //'undefined'
  65. console.log(myTypeof(true)); //'boolean'
  66. //引用值
  67. console.log(myTypeof({})); //'object'
  68. console.log(myTypeof([])); //'array'
  69. console.log(myTypeof(function () { })); //'function'
  70. //包装类
  71. console.log(myTypeof(new Number(1))); //object number
  72. console.log(myTypeof(new String('123'))); //object string
  73. console.log(myTypeof(new Boolean(true))); //object boolean
  1. var test = function a() {
  2. a();
  3. }
  4. //typeof() 加没有声明的变量永远返回undefined
  5. console.log(test.name); //a
  6. console.log(typeof (a)); //undefined 函数表达式忽略函数名称

包装类

  1. console.log(new String('123'));
  2. //转为123
  3. console.log(new Number(456));
  4. //转为456

参数

  1. //在方法中参数不填写值为undefined
  2. console.log(arr, arr.slice(undefined, undefined));
  3. //["a", "b", "c"] ["a", "b", "c"]