Except for basic data (Undefined, Null, Boolean, Number, and String) in Javascript, are other Javascript objects containing "classes?
All objects
In Javascript, except basic data (Undefined, Null, Boolean, Number, String), all others are objects ).
In fact, objects in Javascript are a collection of data and functions. For example, we know:
The Code is as follows:
Var foo = new Function ("alert ('Hello world! ')");
Foo ();
It can be seen that foo is a function and an object. For example:
The Code is as follows:
Function foo (){
// Do something
}
Foo. data = 123;
Foo ["data2"] = "hello ";
Alert (foo. data );
Alert (foo. data2 );
Functions can also add attributes like objects.
Object Construction
Generally, we use constructors to construct objects. However, if there is no constructor, we can also build the desired objects:
The Code is as follows:
Function creatPerson (_ name, _ sex, _ age ){
Return {
Name: _ name,
Sex: _ sex,
Age: _ age,
Get: function (_ key ){
Alert (this [_ key]);
}
};
}
Var Bob = creatPerson ("Bob", "male", 18 );
Bob. get ("name"); // Bob
Bob. get ("sex"); // male
Bob. get ("age"); // 18
But this is not enough. I hope the methods can be shared. For example, if I use this function to create a Tom object, the get function is created again, which seriously wastes my memory.
Import shared resources
Because we know that a function is also an object, we can put the methods or attributes that need to be shared on it ":
The Code is as follows:
Function creatPerson (_ name, _ sex, _ age ){
Var common = arguments. callee. common;
Return {
// Attributes
Name: _ name,
Sex: _ sex,
Age: _ age,
// Method
Sayhi: function () {alert ("hi ");},
// Sharing Method
Get: common. get,
GetType: common. getType,
// Shared attributes
Type: common. type
};
}
CreatPerson. common = {
Get: function (_ key ){
Alert (this [_ key]);
},
GetType: function (){
Alert (this. type );
},
Type: "Person"
};
Var Bob = creatPerson ("Bob", "male", 18 );
Bob. get ("name"); // Bob
Bob. get ("sex"); // male
Bob. getType (); // Person
So we successfully created an object with its own property method and shared property method using a lame method. But in fact, Javascript is so lame to create objects.
In fact, the shared property is not actually implemented because it is still a copy. This is not what we really want to share.
New Keyword
Similar to the above "Object Construction", the purpose of new is to create the object's own attributes and methods. For example:
The Code is as follows:
Function Person (_ name, _ sex, _ age ){
This. name = _ name;
This. sex = _ sex;
This. age = _ age;
This. get = function (_ key ){
Alert (this [_ key]);
};
}
Var Bob = new Person ("Bob", "male", 18 );
Bob. get ("name"); // Bob
Bob. get ("sex"); // male
Bob. get ("age"); // 18
Prototype)
The Javascript author used the method similar to the above "importing shared resources. Since the function is also an object, put the "stuff" that needs to be shared on him:
The Code is as follows:
Function Person (_ name, _ sex, _ age ){
This. name = _ name;
This. sex = _ sex;
This. age = _ age;
This. sayhi = function (_ key ){
Alert ("hi ");
};
}
Person. prototype = {
Constructor: Person,
Get: function (_ key ){
Alert (this [_ key]);
}
};
Var Bob = new Person ("Bob", "male", 18 );
Bob. get ("name"); // Bob
Bob. get ("sex"); // male
Alert (Bob. constructor); // function Person
The model for creating objects in Javascript is concise. new is used to handle their own problems, and prototype is used to handle sharing problems.
If Java objects (instances) are generated by throwing raw materials into a model (class), Javascript objects are generated by giving the materials to the builder (constructor) let him build it by drawing.
Actual Process
Of course, the actual process is not like this. To create an object, you must first process shared resources. For example:
The Code is as follows:
Function (){
Console. dir (this );
Alert (this. type); //
}
A. prototype. type = "";
Var a = new ();
Print a through console. dir. We can see that:
| Type |
"" |
| _ Proto __ |
A {type = ""} |
| Type |
"" |
| Constructor |
A () |
After the constructor creates an object, it immediately assigns its prototype reference to the internal attribute _ proto __of the new object, and then runs the constructor's constructor statements.
Not covered
The Code is as follows:
Function (){
This. type = "B"
}
A. prototype. type = "";
Var a = new ();
Alert (a. type); // B
When we want to get. when type is used, the engine first checks whether the type attribute exists in object a. If yes, the engine returns this attribute. If no, it tries to find whether the type attribute exists in _ proto, if yes, this attribute is returned.
_ Proto _ is not standard. For example, IE does not exist, but IE also has similar internal attributes, but we cannot use it.
For this reason, we can still return a. type When deleting a. type:
The Code is as follows:
Function (){
This. type = "B"
}
A. prototype. type = "";
Var a = new ();
Alert (a. type); // B
Delete a. type;
Alert (a. type); //
Is there any class?
Strictly speaking, Javascript does not have a class.
However, sometimes we use the name of the constructor as the "type not class" Name of the objects created using the constructor ", in order to facilitate the communication in Object-Oriented Programming with Javascript.
The name is just a code, a tool that is easy to understand.
References
Design Idea of Javascript Inheritance Mechanism