问题解决
NaN
0/0 => NaNtypeof(0) => numbertypeof(NaN) => numbertypeof(0/0) => NaN => number
typeof ()返回的是字符串
console.log(typeof (a)); //underfined(string)console.log(typeof ('undefined')); //stringconsole.log(typeof (undefined)); //underfined(string)
//'undefined' -> true//undefined -> false//typeof (a) -> typeof (undefined) -> 'undefined' -> true//(-true) -> -1//(+undefined) -> Number(undefined) -> NaN//(-1 + NaN + '') -> 'NaN' -> true//true && true -> true -> console.log('通过了')if (typeof (a) && (-true) + (+undefined) + '') {console.log('通过了');} else {console.log('没通过');}
实例化之前的prototype和实例化的prototype是有区别的
Car.prototype.name = 'Benz';function Car() {}var car = new Car();//还没实例化Car.prototype = {name: 'Mazda'}console.log(car.name); //Benz//实例化以后function Car() {var this = {__proto__: Car.prototype = {name: 'Benz'}}}
思考:子代赋值是否影响被继承的原型?子代赋值还是父代被赋值?
function Teacher() {this.mSkill = 'JS/JQ';this.success = {alibaba: '28',tencent: '30'}}var teacher = new Teacher();Student.prototype = teacher;function Student() {this.pSkill = 'HTML/CSS'}var student = new Student();student.success.baidu = '100';console.log(teacher);//Professor {mSkill: "JS/JQ", success: {alibaba: "28", tencent: "30", baidu: "100"}}console.log(student);//Student {pSkill: "HTML/CSS"}
结果看出没有赋值到实例对象里而是赋值到实例对象的原型上
//alibabafunction b(x, y, a) {// arguments[2] = 10;// alert(a); //10//形参和实参具有映射关系,谁被改都会更改a = 10;alert(arguments[2]); //10}b(1, 2, 3);//b(1, 2, 3) => b(1, 2, 10)
//undefined > 0 -> false//undefined < 0 -> false//undefined = 0 -> false//null > 0 -> false//null < 0 -> false//null = 0 -> false//undefined == null -> true//undefined === null -> false//isNaN('100') -> false//isNaN('abc') -> true//var num = Number('100') -> isNaN(num) -> false//parseInt('123a') -> 123//parseInt('1a') -> 1console.log(undefined == null); //trueconsole.log(undefined === null); //falseconsole.log(isNaN('100')); //falseconsole.log(parseInt('1a') == 1); //true
var a = 5;function test() {a = 0;console.log(a);console.log(this.a);var a;console.log(a);}test(); //0 5 0new test(); //0 undefined 0
//alibabavar name = '222';var a = {name: '111',say: function () {console.log(this.name);}}var fun = a.say;/*** var fun = function(){* console.log(this.name); //this -> window* }*/fun(); //222a.say(); //111 this-> avar b = {name: '333',say: function (fun) {fun();/*** function(){* console.log(this.name); //this -> window* }()*/}}b.say(a.say); //222b.say = a.say;b.say(); //333//b.say = function(){// console.log(this.name); this -> b//}
var bar = {a: '1'};function test() {bar.a = 'a'; //这里覆盖全局的值Object.prototype.b = 'b';return function inner() {console.log(bar.a); //'a'console.log(bar.b); //'b'}}test()();//此处test()() -> var test = test(); -> test();
//总结知识点//1.函数也是一个对象//2.变量提升//3.函数内部没有var就会提升到全局//4.构造函数实例化内部没有的属性和方法会去访问原型//5.预编译过程:变量,函数声明,函数表达式之间的赋值先后//6.优先级:执行符号()/new + 执行符号() > .调用 > new//7.new + 数字结果 没有输出/*** GO = {* getName: undefined* -> function getName(){console.log(5)}(提升)* -> function(){console.log(4)}(被赋值)* -> function(){console.log(1)}(被没有var声明的getName函数覆盖)* }**///构造函数//3.Foo().getName()//function Foo() {//这里AO需要调用Foo()才能进来//这里getName没有var声明会直接提升到全局getName = function(){console.log(1);}return this;}//Foo函数底下声明getName属性,值为函数//1.Foo.getName();//分析Foo.getName() 只是借用Foo对象里面的方法 如 .length 跟Foo函数本身没有任何关系 JS中函数是一种特殊的对象Foo.getName = function () {console.log(2);}//Foo函数底下原型上声明getName属性,值为函数//(这里需要实例化构造函数Foo()才能访问)Foo.prototype.getName = function () {console.log(3);}//全局声明变量getName接收匿名函数//2.getName()//这里预编译时保持变量getNamevar getName = function () {console.log(4);}//函数声明function getName() {console.log(5);}Foo.getName(); //2//直接访问Foo对象下的getName方法getName(); //4//getName函数提升然后被全局变量getName接收赋值覆盖Foo().getName(); //1//Foo()执行,内部没有var声明的getName函数提升到全局并覆盖之前的值所以打印1getName(); //1//这里执行的是全局的getName 打印1new Foo.getName(); //2//这里.运算 是比new的优先级高的//先执行Foo.getName() 打印2 然后 new 数字2 是没意义的 没有输出结果new Foo().getName(); //3//规律这里执行符号()的优先级比 .大 且执行的同时包含了new//实例化的时候构造函数Foo内部没有this.getName属性,但是原型上有 所以打印原型上的3new new Foo().getName(); //3//情况跟new Foo().getName()一样,然后再new 数字3 没意义没输出//我写的答案: 1 4 1 5 2 1 1//答案:2 4 1 1 2 3 3
//封装myTypeof方法//Object.prototype.toString.call();//原始值://typeof('123') -> 'string';//toString.call('123') -> '[object String]'//typeof(123) -> 'number';//toString.call(123) -> '[object Number]'//typeof(null) -> 'object';//toString.call(null) -> '[object Null]'//typeof(NaN) -> 'number';//toString.call(NaN) -> '[object Number]'//typeof(undefined) -> 'undefined';//toString.call(undefined) -> '[object Undefined]'//typeof(true) -> 'boolean';//toString.call(true) -> '[object Boolean]'//引用值//typeof({}) -> 'object';//toString.call({}) -> '[object Object]'//typeof([]) -> 'object';//toString.call([]) -> '[object Array]'//typeof(function(){}) -> 'function';//toString.call(function(){}) -> '[object Function]'//包装类//typeof(new Number(1)) -> 'object';//toString.call(new Number(1)) -> '[object Number]'//typeof(new String('1')) -> 'object';//toString.call(new String('123')) -> '[object String]'//typeof(new Boolean(true)) -> 'object';//toString.call(new Boolean(true)) -> '[object Boolean]'function myTypeof(val) {var toStr = Object.prototype.toString,type=typeof(val),res = toStr.call(val);//包装类和 引用值 null {} []if (type === 'object') {switch (res) {case '[object Null]':type = 'null';break;case '[object Object]':type = 'object';break;case '[object Array]':type = 'array';break;case '[object Number]':type = 'object number';break;case '[object String]':type = 'object string';break;case '[object Boolean]':type = 'object boolean';break;}}return type;}//原始值console.log(myTypeof('123')); //'string'console.log(myTypeof(123)); //'number'console.log(myTypeof(null)); //'null'console.log(myTypeof(NaN)); //'number'console.log(myTypeof(undefined)); //'undefined'console.log(myTypeof(true)); //'boolean'//引用值console.log(myTypeof({})); //'object'console.log(myTypeof([])); //'array'console.log(myTypeof(function () { })); //'function'//包装类console.log(myTypeof(new Number(1))); //object numberconsole.log(myTypeof(new String('123'))); //object stringconsole.log(myTypeof(new Boolean(true))); //object boolean
var test = function a() {a();}//typeof() 加没有声明的变量永远返回undefinedconsole.log(test.name); //aconsole.log(typeof (a)); //undefined 函数表达式忽略函数名称
包装类
console.log(new String('123'));//转为123console.log(new Number(456));//转为456
参数
//在方法中参数不填写值为undefinedconsole.log(arr, arr.slice(undefined, undefined));//["a", "b", "c"] ["a", "b", "c"]
