8.7w+
社区成员
const A=(function(){
return {
fn:function(){
const A=()=>{};
A.prototype={
constructor:function(){
this.a=1;
}
};
A.getInstance=()=>{
if(this.instanceA===undefined){
this.instanceA=Object.create(A.prototype);
A.prototype.constructor.call(this.instanceA);
}
return this.instanceA;
}
return A;
}
}.fn();
})();
const A=()=>{};
A.prototype={
constructor:function(){
this.a=1;
}
};
A.getInstance=()=>{
if(this.instanceA===undefined){
this.instanceA=Object.create(A.prototype);
A.prototype.constructor.call(this.instanceA);
}
return this.instanceA;
}
> aa=A.getInstance()
< constructor {a: 1}
> bb=A.getInstance()
< constructor {a: 1}
> aa===bb
< true
> cc=new A()
VM3645:1 Uncaught TypeError: A is not a constructor
at <anonymous>:1:1function f1() {
console.log('f1');
}
var f2 = function () {
console.log('f2')
}
var f3 = f1;
f3();
console.log(f3);
var f4 = f1();
/* f4(); */
console.log(f4);
var f5 = f2;
f5();
console.log(f5);
var f6 = f2();
/* f6(); */
console.log(f6);
(
f1
);
(
f1()
);
var b = {}
b.bb = function() {
console.log(this)
}
a = b.bb
a() // 打印window
// 这里的a赋值只是把函数的值给弄过来,并没有说他的this就指向b
// 一个很简单的道理就是引用传递和值传递