The ES5 syntax does not support class classes, but you can declare a class by clearing the function as follows:
function person (name) {
This.name=name;
}
var john=new person (' John ');
Console.log (john.name);//john
But this class can be called directly like function execution: person ()
The decision is not to be called by New or () so modify the above class:
function person (name) {
This.name=name;
if (this instanceof person) {
Alert (' New call ');
}else{
Alert (' function call ');
}
}
New person (' Xiaoqiang ')//=> new call
Person (' Xiaoqiang ')//=> function call
Also in the wording can be easily copied and pasted into any class inside the following:
Notation 1:
function person (name) {
This.name=name;
if (this instanceof Arguments.callee) {
Alert (' New call ');
}else{
Alert (' function call ');
}
}
New person (' Xiaoqiang ')//=> new call
Person (' Xiaoqiang ')//=> function call
Notation 2:
function person (name) {
This.name=name;
if (this.constructor = = = Arguments.callee) {
Alert (' New call ');
}else{
Alert (' function call ');
}
}
New person (' Xiaoqiang ')//=> new call
Person (' Xiaoqiang ')//=> function call
It looks like the top three types are perfect, but how to call you will blind you
var jack=new person (' Jack '); =>new Call
Jack.f=person
Jack.f (' Don't believe you try ')//=> new call
ES5 syntax, how JavaScript determines whether a function is new or () called