案例1: //考点:this 作用域 隐式声明提升 原型 符号优先级
function Foo() {
getName = function () {
console.log(1);
}
return this;
}
Foo.getName = function () {
console.log(2)
}
Foo.prototype.getName = function () {
console.log(3)
}
var getName = function () {
console.log(4)
}
function getName() {
console.log(5)
}
Foo.getName();
getName();
Foo().getName();
getName();
new Foo.getName();
new Foo().getName();
new new Foo().getName();
代码分析
var getName;
function Foo() {
getName = function () {
console.log(1);
}
return this;
}
function getName() {
console.log(5)
}
var getName = function () {
console.log(4)
}
Foo.getName = function () {
console.log(2)
}
Foo.prototype.getName = function () {
console.log(3)
}
Foo.getName();//直接就近运行 2
getName();//同理4
Foo().getName();//先取Foo函数的返回值 window
// var getName = function () {
// console.log(1)
//}
//window.getName() 1
getName();// 1
new Foo.getName();
//new (Foo.getName)() 2
new Foo().getName();// (new Foo()).getName()
// __prto__:{getName = function () {
// console.log(3)
//}} 3
new new Foo().getName();
//new ((new Foo()).getName)();
//3
案例2://符号优先级,new fn 和new fn()
function fn () {
return function(){console.log(111);return[10,20,30]}//[10,3,20]
}
var re=new fn
console.log(re)//function(){console.log(111);return[10,20,30]}
var re2=fn()
console.log(re2)//function(){console.log(111);return[10,20,30]}
var re3=new fn()//new (fn()) (new fn)() (new fn())
console.log(re3)//function(){console.log(111);return[10,20,30]}
var re4=new re3
console.log(re4)//111,[10,20,30]
var re5=new re3()
console.log(re5)
案例3://变量提升问题
var fn1 = 'ivan';
var name = 'good';
var fn1 = function(y) {
var name='123';
y();
}
function fn1(x) {
x(name);
}
function fn2(x) {
console.log(x); //udefined
console.log(name); //因为下边声明了name 所以为 undefined
var name = 'hello';
console.log(name); // hello
}
fn1(fn2);
案例4:
var num = 5;
function func1() {
var num = 3;
var age = 4;//6
function func2() {
console.log(num);//unf
var num = 'ivan';
function func3() {
age = 6;
}
func3();
console.log(num);// 'ivan'
console.log(age);//6
}
func2();
}
func1();
案例5:对象引用的内存空间
function changeObjectProperty(o) {
o.siteUrl = "http://www.csser.com/";
o = new Object(); //让o重新引用一个对象
o.siteUrl = "http://www.popcg.com/";
return o
}
var CSSer = new Object();
var re=changeObjectProperty(CSSer); //将CSSer传入==>使CSSer和 o引用同一个对象
console.log(CSSer.siteUrl);//http://www.csser.com/
console.log(re.siteUrl)//http://www.popcg.com/
re=null;//释放空间,避免内存泄漏
案例6:
function fun(n, o) {
console.log(o)
return {
fun: function(m) {
return fun(m, n);
}
};
}
var a = fun(0);
a.fun(1);
a.fun(2);
a.fun(3);
var b = fun(0).fun(1).fun(2).fun(3);
var c = fun(0).fun(1);
c.fun(2);
c.fun(3);
代码分析
全局socoped:{
var a=fun(0)
socoped[fun(0)]:{
var n=0;
var o;
console.log(o)//打印undef
return {fun: function(m) {return fun(m, n)}}
socoped[a.fun(1)]:{
var m=1;
return fun(m, n)==> fun(1,0)
}
socoped[a.fun(2)]:{
var m=2
return fun(m, n)==>fun(2,0)
}
socoped[a.fun(3)]{
var m=3;
return fun(m, n)==>fun(3,0)
}
}
a.fun(1);
socoped[a.fun(1)--fun(1,0)]:{
var n=1
var o=0
console.log(o)//打印0
return {fun: function(m) {return fun(m, n)}}
}
a.fun(2);
socoped:[a.fun(2)--fun(2,0)]{
var n=2
var o=0
console.log(o)//打印0
return {fun: function(m) {return fun(m, n)}}
}
a.fun(3);
socoped:[a.fun(3)--fun(3,0)]{
var n=3
var o=0
console.log(o)//打印0
return {fun: function(m) {return fun(m, n)}}
}
}
var b=fun(0).fun(1).fun(2).fun(3)
全局scoped:{
var b=fun(0)
scoped[fun(0)]{
var n=0
var o=undf
console.log(o)//打印undf
return {fun: function(m) {return fun(m, n)}}
scoped[fun(0).fun(1)]{
var m=1
return fun(m, n)==>fun(1,0)
}
}
scoped[fun(0).fun(1)--fun(1,0)]{
var n=1
var o=0
console.log(o)//打印0
return {fun: function(m) {return fun(m, n)}}
scoped[fun(0).fun(1).fun(2)]{
var m=2
return fun(m, n)==>fun(2,1)
}
}
scoped[fun(0).fun(1).fun(2)--fun(2,1)]{
var n=2
var o=1
console.log(o)//打印1
return {fun: function(m) {return fun(m, n)}}
scoped[fun(0).fun(1).fun(2).fun(3)]{
var m=3
return fun(m, n)==>fun(3,2)
}
}
scoped[fun(0).fun(1).fun(2).fun(3)--fun(3,2)]{
var n=3
var o=2
console.log(o)//打印2
return {fun: function(m) {return fun(m, n)}}
}
}