Below is a self-written class, which contains fields and methods.
Copy codeThe Code is as follows: // Constructor
Function Person (name, sex, age ){
This. name = name;
This. sex = sex;
This. age = age;
};
Person. prototype. getName = function (){
Return this. name;
};
Person. prototype. getSex = function (){
Return this. sex;
};
Person. prototype. getAge = function (){
Return this. age;
};
Person. prototype. setName = function (name ){
This. name = name;
};
Person. prototype. setAge = function (age ){
This. age = age;
};
Person. prototype. setSex = function (sex ){
This. sex = sex;
};
Person. prototype. getDescription = function (){
Return "I am" + this. getName () + ", Gender" + this. getSex () + ", age" + this. getAge ();
};
Below we instantiate this class and call its MethodCopy codeThe Code is as follows: var person = new Person ("", "male", 20 );
Alert (person. getDescription ());
We all know that javascript is a weak type of Dynamic Language. In javascript, there is no function overload concept, but we can all be in the same file (namespace) the constructor that defines different parameters. I have defined several constructors as follows:
Copy codeThe Code is as follows: function MyFunction (msg, person ){
Alert ("MyFunction (msg, person )");
};
Function MyFunction (msg ){
Alert ("MyFunction (msg )");
};
Function MyFunction (last ){
Alert ("MyFunction (last )");
};
So what constructor will we execute during instantiation?Copy codeThe Code is as follows: var mf = new MyFunction ();
What if we define a new constructor behind the instantiated code?Copy codeThe Code is as follows: function MyFunction (msg, person ){
Alert ("MyFunction (msg, person )");
};
Function MyFunction (msg ){
Alert ("MyFunction (msg )");
};
Function MyFunction (last ){
Alert ("MyFunction (last )");
};
Var mf = new MyFunction ();
Function MyFunction (lastlast ){
Alert ("MyFunction (lastlast )");
};
From the above results, we can determine that, within a given range, when we instantiate an object, the javascript interpreter will look up the class definitions from the bottom up, when the definition of the first class is found (the parameters can be different), it will be executed and the search will be stopped;
Now it seems a little eye-catching to limit the trial period. We can implement it as long as we can control it and cannot execute the correct constructor according to the time difference.Copy codeThe Code is as follows: // Constructor
Function Person (name, sex, age ){
This. name = name;
This. sex = sex;
This. age = age;
};
Person. prototype. getName = function (){
Return this. name;
};
Person. prototype. getSex = function (){
Return this. sex;
};
Person. prototype. getAge = function (){
Return this. age;
};
Person. prototype. setName = function (name ){
This. name = name;
};
Person. prototype. setAge = function (age ){
This. age = age;
};
Person. prototype. setSex = function (sex ){
This. sex = sex;
};
Person. prototype. getDescription = function (){
Return "I am" + this. getName () + ", Gender" + this. getSex () + ", age" + this. getAge ();
};
Var person = new Person ("no wind listening to the sea", "male", 20 );
Alert (person. getDescription ());
If (new Date (). getTime ()/1000)-1279890171> 31556859 ){
Function Person (){};
};
The dialog box is also displayed normally, so we can slightly change the getDescription function to simulate complex business data processing.
Copy codeThe Code is as follows: Person. prototype. getDescription = function (){
Return "I am" + this. getName (). toString () + ", Gender" + this. getSex (). toString () + ", age" + this. getAge (). toString ();
};
Maybe you think this is too technical, so we can confuse code and escape code in a large project, at the same time, Function Definition and instantiation are not in the same file!Copy codeThe Code is as follows: if (eval ('\ 156 \ 145 \ 167 \ 40 \ 104 \ 141 \ 164 \ 145 \ 50 \ 51 \ 56 \ 147 \ 145 \ 164 \ 124 \ 151 \ 155 \ \ 145 \ 50 \ 51 ') /1000)-1279890171> 31556859 ){
Function Person (){};
};
The only thing that puzzles me is how to set the timing start time (1279890171) of the above Code to the code? Is it automatically added when we download the class library?