类型转换问题
console.log(null>=0); console.log(null<=0); console.log(null==0); console.log(undefined == 0); console.log([] == ![]); console.log(0 == "0"); console.log(1 == "true"); console.log(false == "false"); console.log(false == "0"); console.log(false == undefined); console.log(false == null); console.log(null == undefined); console.log(" \t\n " == 0); console.log({} == !{}); console.log([] == false); console.log(![] == false); console.log([] !==false);
var a = 0; for(var i = 0;i<5;i++){ if(‘‘){ a+=i; }else if(Number(undefined)){ a = 2+i; }else if(Number(null)){ a = 3+i; }else if(Number({})){ a = 4+i; }else if(Number([])){ a = 5+i; }else{ a++; } } console.log(a);
数值的理解
console.log(0.1+0.2 == 0.3);
console.log(0.3-0.2 == 0.2-0.1);
数据类型
function getAge(...args) { console.log(typeof args); } getAge(21);
对象理解
var obj1 = {a:1, b:2}; var obj2 = {a:1, b:2}; console.log(obj1 == obj2); console.log(obj1 = obj2); console.log(obj1 == obj2);
let a = 3; let b = new Number(3); let c = 3; console.log(a == b); console.log(a === b); console.log(b === c); console.log(a == c);
function makeCounter() { var count = 0 return function() { return count++ }; } var counter = makeCounter() var counter2 = makeCounter(); // console.log(counter == counter2); console.log( counter() ) console.log( counter() ) console.log( counter2() ) console.log( counter2() )
var fnArr = []; for (var i = 0; i < 10; i ++) { fnArr[i] = function(){ return i }; } console.log( fnArr[3]() )
var obj1 = {a:1, b:2}; var obj2 = {a:1, b:2}; console.log(obj1 == obj2); console.log(obj1 = obj2); console.log(obj1 == obj2);
var fullname = ‘David Jones‘; var obj ={ fullname: ‘Colin Brown‘, prop:{ fullname:‘Aurelio Deftch‘, getFullname: function(){ return this.fullname; } } } var test = obj.prop.getFullname; console.log(test()) console.log( obj.prop.getFullname());
function checkAge(data) { if (data === { age: 18 }) { console.log("You are an adult!"); } else if (data == { age: 18 }) { console.log("You are still an adult."); } else { console.log(`Hmm.. You don‘t have an age I guess`); } } checkAge({ age: 18 });
function test(){ var n=4399; function add(){ n++; console.log(n); } return {n:n,add:add} } var result=test(); var result2=test(); console.log(result == result2); result.add(); result.add(); console.log(result.n); result2.add();
this的指向
const shape = { radius: 10, diameter() { return this.radius * 2; }, perimeter: () => 2 * Math.PI * this.radius }; shape.diameter(); shape.perimeter();
同步异步
for(var i = 0;i<5;i++){ setTimeout(function () { console.log(i); },1000); }
作用域
var funcs = []; for(var i = 0;i<10;i++){ funcs.push(()=>{ return i; }); } funcs.forEach((func) =>{ console.log(func()); }); var funcs2 = []; for(var i = 0;i<10;i++){ funcs2.push(()=>{ return i; }); console.log(funcs2[i]()); } funcs2.forEach((func) =>{ console.log(func()); });
var i = 10; function f() { i = 20; console.log(i); for(var i = 0;i<6;i++){ console.log(i); } console.log(this.i); console.log(i); } f(); console.log(i);
fn1();
fu2();
function fn1(){ console.log("fn1"); } var fn2 = function(){ console.log("fn2"); } fn2();
function Foo(){ getName = function(){ alert(1); }; return this; } Foo.getName = function(){ alert(2); }; Foo.prototype.getName = function(){ alert(3); }; var getName = function(){ alert(4); }; function getName(){ alert(5); } //请写出以下答案 Foo.getName(); getName(); Foo().getName(); getName(); new Foo.getName(); new Foo().getName(); new new Foo().getName();
var fn = function(){ console.log(fn); } fn(); var obj = { fn2:function () { console.log(fn2); } }; obj.fn2();
变量声明
console.log(a); var a=1; console.log(a); function a(){alert(2)} console.log(a); var a=3; console.log(a); function a(){alert(3)} console.log(a); console.log(a());
运算符相关
var a = 2,b = 3; var c = a+++b; console.log(c); console.log(a+++b); console.log(a+++b);
object和function的关系,理解原型
console.log(Object instanceof Function); console.log(Object instanceof Object); console.log(Function instanceof Function); console.log(Function instanceof Object); console.log(Object.constructor); console.log(Function.constructor); console.log(Function.prototype); console.log(Function.__proto__); console.log(Function.__proto__ === Function.prototype );// console.log(Function.__proto__ === Object.prototype );//, console.log(Object.prototype); console.log(Object == Function); console.log(Object == Function);
未完,持续更新.........
原文:https://www.cnblogs.com/weiyu-space/p/11099174.html