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

var str = 'abc';str += 1;var test = typeof (str); //'string'if (str.lengtn == 6) {//new String('string').sign="xxxx";立即销毁test.sign = "typeof的返回结果可能为string";}//访问: new String('string').sign;上面new出来的已销毁,此时是新对象,没有sign属性console.log(test.sign); //undefined/*----------------------------------------------------------------------------------------------*/var a = 5;function testFun() {// new: var this={}a = 0;console.log(a);console.log(this.a);var a;console.log(a);}// testFun();// 0 5 0 没有new的时候window指的是全局对象windownew testFun(); // 0 undefined 0/*--------------------------------------------------------------------------------*/function employee(name, code) {this.name = "张三";this.code = "A001";}var newmp = new employee("李四", "A002");console.log(newmp.name); //张三console.log(newmp.code); //A001/*--------------------------------------------------------------------------------*/function Person(name, age, sex) {var a = 0;this.name = name;this.age = age;this.sex = sex;function sss() {a++;console.log(a);}//闭包,把内部函数返回到外部this.say = sss;}var oPerson = new Person();/*** 第一次new一个对象,执行function Person(){}函数体,且与function say(){}形成闭包,* 两次say共用一个a*/oPerson.say(); //1oPerson.say(); //2/*** 第二次new一个对象,重新 执行function Person(){}函数体,此时a=0,之后的闭包再0基础加1*/var oPerson1 = new Person();oPerson1.say(); //1/*--------------------------------------------------------------------------------*/var x = 1,y = z = 0;function add(n) {return n + 1;}y = add(x); //4function add(n) {return n + 3;}z = add(x); //4console.log(x); //1console.log(y); //4console.log(z); //4/*--------------------------------------------------------------------------------*/function d(x) {console.log(arguments);return x;}(1, 2, 3, 4, 5);//(1,2,3,4,5);自动分离开,所以不调用,也不报错/*--------------------------------------------------------------------------------*/console.log(parseInt(3, 8)); //3 3当作8进制数,转为十进制console.log(parseInt(3, 2)); //NaN 3当作二进制数,但3不是二进制console.log(parseInt(3, 0)); // 3/*--------------------------------------------------------------------------------*/function b(x,y,a){arguments[2]=10;console.log(a);//10}b(1,2,3);//参数与arguments是互相映射关系,不是一个东西,但同时改变function bb(x,y,a){a=10;console.log(arguments[2]);//10}bb(1,2,3)
2.原型、原型链
3.小知识注意点
|
1. 我们可以操作小数点前16位和小数点后16位
1. 引用值比对的是地址 {}=={}是错误的,他们的地址不同,是两个空间
var obj={}; var obj1=obj; obj==obj1;true 因为他们指向同一个内存空间 | | |
| —- | —- | —- |
|
|
4. this的面试题
**注意:Number是严格匹配转化**
//模拟isNaNfunction myIsNaN(ret){var ret=Number(ret)ret=ret+''if(ret=='NaN'){return true}return false}console.log( myIsNaN("100"));console.log( myIsNaN("100abc"));console.log( myIsNaN(NaN));//面试题var name="222";var a={name:"111",say:function(){console.log(this.name);}}var fun=a.say;fun();//222----->fun没有人调用,里面的this是全局windowa.say();//111---->谁调用这个方法,this就是谁var b={name:"333",say:function(fun){//b.say()调用,this--->b//但是这里是fun(),而不是this.fun(),所以fun()里面的this是全局windowfun();}}b.say(a.say)//222---->入参a.say意味着把函数体拿过来了,其他没关系// 把a的方法体给bb.say=a.sayb.say();//333//面试题3var foo='123';function print(){// 里面的this为windowvar foo="456";this.foo="789";//this.foo改变的为全局的fooconsole.log(foo);//自己函数里有foo,则使用自己的foo}print()//456//面试题4/*虽然new print();此时print()里面的this不再是window,但是我们访问的是foo,而不是this.foo所以打印的为全局的foo*/var foo=123;function print() {this.foo=234;console.log(foo);}new print()//123//面试题5/*** 1.test(); 预编译全局a=5,test()自己的a=0,打印的a都为0;因为没有其他对象调用test,* 所以test()里的this为window,this.a打印的全局a=5* 2. new test();test()里面的this不再是window,谁调用就是谁* AO{a=0;this={}},因为没有this.a=xx赋值,所以this.a=undefined*/var a = 5;function test() {//new 的时候 隐式:var this={__proto__:test.prototype}通过this.xxx象里面赋值a = 0;console.log(a);console.log(this.a);var a;console.log(a);}// test();//0 5 0new test();// 0 undefined 0//面试题6function print(){console.log(foo);//undefinedvar foo=2;console.log(foo);//2//hello变量是未定义,但typeof一个未声明的变量得到undefined,不会报错console.log(hello); //hello is not defined}print()//面试7function print() {var marty = {name: 'marty',printName: function () {console.log(this.name);}}var test1 = {name: "test1"}var test2 = {name: "test2"}var test3 = {name: "test3"}test3.printName = marty.printName;var printName2 = marty.printName.bind({name: 123});marty.printName.call(test1);//把marty.printName的this指向改为test1,再执行marty.printName.apply(test2);// 把marty.printName的this指向改为test2,再执行marty.printName();//此时打印的为marty自己啊的nametest3.printName();//test3}print()//test1 test2 marty test3//面试题8
5. delete删除
//1. 面试题1var s = (function (x) {//形参的x,相当于var x; var出来的变量是无法删除的delete xreturn x}(1))console.log(s); //1//2.面试题2var h=function a(){return 23;}//匿名函数表达式,后面的函数名是无效的console.log(typeof a());//a is not defined

