1. /**
  2. * 这里涉及立即执行函数和逗号运算符,先执行逗号运算符,即逗号的最后一个值被返回
  3. * 立即执行函数立即执行g(),结果返回: 2----->f=2
  4. */
  5. var f = (function f() {return "1";}, function g() {return 2;})();
  6. console.log(typeof f);// "number"
  1. /**
  2. * 注意:(function ff(){})把函数声明使用()括起来变成了表达式,此时ff函数名被忽略,没有这个ff
  3. * 虽然ff未定义,但是typeof 一个未定义的变量会返回字符串undefined
  4. */
  5. var x = 1;
  6. if (function ff() {}) {
  7. x += typeof ff;
  8. }
  9. console.log(x); //1undefined

1.对象、包装类

image.png

  1. var str = 'abc';
  2. str += 1;
  3. var test = typeof (str); //'string'
  4. if (str.lengtn == 6) {
  5. //new String('string').sign="xxxx";立即销毁
  6. test.sign = "typeof的返回结果可能为string";
  7. }
  8. //访问: new String('string').sign;上面new出来的已销毁,此时是新对象,没有sign属性
  9. console.log(test.sign); //undefined
  10. /*----------------------------------------------------------------------------------------------*/
  11. var a = 5;
  12. function testFun() {
  13. // new: var this={}
  14. a = 0;
  15. console.log(a);
  16. console.log(this.a);
  17. var a;
  18. console.log(a);
  19. }
  20. // testFun();// 0 5 0 没有new的时候window指的是全局对象window
  21. new testFun(); // 0 undefined 0
  22. /*--------------------------------------------------------------------------------*/
  23. function employee(name, code) {
  24. this.name = "张三";
  25. this.code = "A001";
  26. }
  27. var newmp = new employee("李四", "A002");
  28. console.log(newmp.name); //张三
  29. console.log(newmp.code); //A001
  30. /*--------------------------------------------------------------------------------*/
  31. function Person(name, age, sex) {
  32. var a = 0;
  33. this.name = name;
  34. this.age = age;
  35. this.sex = sex;
  36. function sss() {
  37. a++;
  38. console.log(a);
  39. }
  40. //闭包,把内部函数返回到外部
  41. this.say = sss;
  42. }
  43. var oPerson = new Person();
  44. /**
  45. * 第一次new一个对象,执行function Person(){}函数体,且与function say(){}形成闭包,
  46. * 两次say共用一个a
  47. */
  48. oPerson.say(); //1
  49. oPerson.say(); //2
  50. /**
  51. * 第二次new一个对象,重新 执行function Person(){}函数体,此时a=0,之后的闭包再0基础加1
  52. */
  53. var oPerson1 = new Person();
  54. oPerson1.say(); //1
  55. /*--------------------------------------------------------------------------------*/
  56. var x = 1,
  57. y = z = 0;
  58. function add(n) {
  59. return n + 1;
  60. }
  61. y = add(x); //4
  62. function add(n) {
  63. return n + 3;
  64. }
  65. z = add(x); //4
  66. console.log(x); //1
  67. console.log(y); //4
  68. console.log(z); //4
  69. /*--------------------------------------------------------------------------------*/
  70. function d(x) {
  71. console.log(arguments);
  72. return x;
  73. }(1, 2, 3, 4, 5);
  74. //(1,2,3,4,5);自动分离开,所以不调用,也不报错
  75. /*--------------------------------------------------------------------------------*/
  76. console.log(parseInt(3, 8)); //3 3当作8进制数,转为十进制
  77. console.log(parseInt(3, 2)); //NaN 3当作二进制数,但3不是二进制
  78. console.log(parseInt(3, 0)); // 3
  79. /*--------------------------------------------------------------------------------*/
  80. function b(x,y,a){
  81. arguments[2]=10;
  82. console.log(a);//10
  83. }
  84. b(1,2,3);
  85. //参数与arguments是互相映射关系,不是一个东西,但同时改变
  86. function bb(x,y,a){
  87. a=10;
  88. console.log(arguments[2]);//10
  89. }
  90. bb(1,2,3)

2.原型、原型链

image.pngSnipaste_2021-01-25_00-26-26.png

3.小知识注意点

|
1. 我们可以操作小数点前16位和小数点后16位
1. 引用值比对的是地址 {}=={}是错误的,他们的地址不同,是两个空间
var obj={}; var obj1=obj; obj==obj1;true 因为他们指向同一个内存空间 | | | | —- | —- | —- | | |

4. this的面试题

**注意:Number是严格匹配转化**

  1. //模拟isNaN
  2. function myIsNaN(ret){
  3. var ret=Number(ret)
  4. ret=ret+''
  5. if(ret=='NaN'){
  6. return true
  7. }
  8. return false
  9. }
  10. console.log( myIsNaN("100"));
  11. console.log( myIsNaN("100abc"));
  12. console.log( myIsNaN(NaN));
  13. //面试题
  14. var name="222";
  15. var a={
  16. name:"111",
  17. say:function(){
  18. console.log(this.name);
  19. }
  20. }
  21. var fun=a.say;
  22. fun();//222----->fun没有人调用,里面的this是全局window
  23. a.say();//111---->谁调用这个方法,this就是谁
  24. var b={
  25. name:"333",
  26. say:function(fun){
  27. //b.say()调用,this--->b
  28. //但是这里是fun(),而不是this.fun(),所以fun()里面的this是全局window
  29. fun();
  30. }
  31. }
  32. b.say(a.say)//222---->入参a.say意味着把函数体拿过来了,其他没关系
  33. // 把a的方法体给b
  34. b.say=a.say
  35. b.say();//333
  36. //面试题3
  37. var foo='123';
  38. function print(){
  39. // 里面的this为window
  40. var foo="456";
  41. this.foo="789";//this.foo改变的为全局的foo
  42. console.log(foo);//自己函数里有foo,则使用自己的foo
  43. }
  44. print()//456
  45. //面试题4
  46. /*
  47. 虽然new print();此时print()里面的this不再是window,但是我们访问的是foo,
  48. 而不是this.foo所以打印的为全局的foo
  49. */
  50. var foo=123;
  51. function print() {
  52. this.foo=234;
  53. console.log(foo);
  54. }
  55. new print()//123
  56. //面试题5
  57. /**
  58. * 1.test(); 预编译全局a=5,test()自己的a=0,打印的a都为0;因为没有其他对象调用test,
  59. * 所以test()里的this为window,this.a打印的全局a=5
  60. * 2. new test();test()里面的this不再是window,谁调用就是谁
  61. * AO{a=0;this={}},因为没有this.a=xx赋值,所以this.a=undefined
  62. */
  63. var a = 5;
  64. function test() {
  65. //new 的时候 隐式:var this={__proto__:test.prototype}通过this.xxx象里面赋值
  66. a = 0;
  67. console.log(a);
  68. console.log(this.a);
  69. var a;
  70. console.log(a);
  71. }
  72. // test();//0 5 0
  73. new test();// 0 undefined 0
  74. //面试题6
  75. function print(){
  76. console.log(foo);//undefined
  77. var foo=2;
  78. console.log(foo);//2
  79. //hello变量是未定义,但typeof一个未声明的变量得到undefined,不会报错
  80. console.log(hello); //hello is not defined
  81. }
  82. print()
  83. //面试7
  84. function print() {
  85. var marty = {
  86. name: 'marty',
  87. printName: function () {
  88. console.log(this.name);
  89. }
  90. }
  91. var test1 = {
  92. name: "test1"
  93. }
  94. var test2 = {
  95. name: "test2"
  96. }
  97. var test3 = {
  98. name: "test3"
  99. }
  100. test3.printName = marty.printName;
  101. var printName2 = marty.printName.bind({
  102. name: 123
  103. });
  104. marty.printName.call(test1);//把marty.printName的this指向改为test1,再执行
  105. marty.printName.apply(test2);// 把marty.printName的this指向改为test2,再执行
  106. marty.printName();//此时打印的为marty自己啊的name
  107. test3.printName();//test3
  108. }
  109. print()//test1 test2 marty test3
  110. //面试题8

5. delete删除

  1. //1. 面试题1
  2. var s = (function (x) {
  3. //形参的x,相当于var x; var出来的变量是无法删除的
  4. delete x
  5. return x
  6. }(1))
  7. console.log(s); //1
  8. //2.面试题2
  9. var h=function a(){
  10. return 23;
  11. }
  12. //匿名函数表达式,后面的函数名是无效的
  13. console.log(typeof a());//a is not defined