The core of JS is the object
{},
The new function () {} is also an object in this form.
http://www.nowamagic.net/librarys/veda/detail/241
Tidy up some information on the Internet for reference
1. Create an Object
1.1 New
To create an object with new:
[JavaScript]View Plaincopyprint?
- var user = new Object ();
- User.age = 12; //Add properties to an object at the same time
- User.Name = ' Ajun ';
1.2{}
Create an object with {}, such as:
[JavaScript]View Plaincopyprint?
- var user = {
- ' Name ': ' Ajun,
- ' Age ': 12
- }
At the same time, we have added two attributes for User: Name,age
In the above code, you can also add a method to an object, such as:
[JavaScript]View Plaincopyprint?
- var user = {
- ' Name ': ' Ajun ',
- ' Age ': 12
- ' Say ':function () {
- Alert (this.name); This represents the current object
- }
- }
2. Classes and constructors
2.1 How to define
In JavaScript, all variables and methods are objects that can be used to pass arguments to each other.
Look at the following method:
[JavaScript]View Plaincopyprint?
- function User (name,age) {
- this.name = name;
- this.age = age;
- This.say = function () {
- Alert (this.name+ ' say hello!! ');
- }
- }
At this point you can understand that the user you can see the name of a class, and user () is the class to construct the method, which is a bit similar to the Java class and constructor method must have the same name, in the new call its construction method, some initialization operations, can be placed within your constructor method, Here we are used to initialize the value of the name and age property, and the following creates the code for the user object:
[JavaScript]View Plaincopyprint?
- var user = New user (' Ajun ', 24);
- User.say (); //ajun say hello!!
Here is the following explanation:
When we are in the new object, we actually invoke a called call (), the current object is passed as a parameter, assigned to this, so this refers to the current reference object
3. Prototypes
3.1 Prototype
In JavaScript, each method is named "Prototype" and is used to refer to the prototype object in the following code:
[JavaScript]View Plaincopyprint?
- function user () {
- this.name = ' Ajun ';
-
- this.say = function () {
- alert ( this.name+ ' say hello!! ');
- }
- }  
This user will have a prototype attribute, which can be user.prototype when you refer to it, and this object will inherit from user when you new user object. Prototype all the attributes while the user. Prototype also inherits all of the attributes from Object.prototype, so you can call ToString () on your object, in fact he is an object. The prototype property is only inherited by your object, where you can understand that Java classes inherit, and subclasses inherit the parent class.
With the concept of prototype, we can add methods and properties to user through prototype, so that each user object will share methods and properties later, instead of having their copy for each object.
[JavaScript]View Plaincopyprint?
- function User () {
- this.name = ' Ajun ';
- this.age = 24;
- }
- User. Prototype. Say = function () {
- Alert (this.name+ ' say hello!! ');
- }
- var user = new user ();
- User.say ();
- var user2 = new User ();
- User2.say ();
So that when we finish the new user object, when we call this method, he can continue to use it for other methods.
3.2 Prototype chain
Each JavaScript object inherits a prototype chain, and all prototypes are terminated by Object.prototype. Note that the inheritance that you see so far is the inheritance between the active objects. It differs from the common concept of inheritance, which refers to the occurrence of inheritance between classes when declaring a class. As a result, JavaScript inheritance is more dynamic. It does this with a simple algorithm, as follows: When you try to access the properties/methods of an object, JavaScript checks to see if the property/method is defined in the object. If not, the prototype of the object is checked. If not, the prototype of the object is checked, so continue, checking to Object.prototype.
The way that JavaScript dynamically parses property access and method calls produces some special effects:
The changes made to the prototype are immediately rendered on objects that inherit the prototype object, even after the objects have been created.
@ If a property/method X is defined in an object, the property/method with the same name will be hidden in the object's prototype. For example
You can override the ToString method of Object.prototype by defining the ToString method in User.prototype.
The @ change is passed only in one direction, that is, from the prototype to its derived object, but not in the opposite direction.
Example:
[JavaScript]View Plaincopyprint?
- function user () {
- this.name = ' Ajun ';
-
- }
- user. prototype. tostring = function () {
- alert (this.name+ ' say hello!! ');
- }  
- var user = new user ();
- user. tostring ();
At this point, the ToString of Object.prototype will be overwritten. This will not invoke Object.prototype's ToString, it will not output [object Object], and the output is Ajun say hello!! The
4. Static Properties and methods
Sometimes, you want to like in Java, through the class directly to manipulate your properties and methods, in Java these are static properties, directly through the class name to refer to, in JavaScript can also be done, see the following code:
[JavaScript]View Plaincopyprint?
- function User () {}
- User.age = 12;
- User.Name = ' Ajun ';
- User.say = function () {
- return ' Ajun ';
- }
- Alert (User.say ());
You can then refer directly to your method or property using the method name instead of the new object.
5. Private properties
Under normal circumstances, you cannot access local variables inside a function from outside of a function. After the function exits, the local variable will disappear forever for various practical reasons. However, if the local variable is caught by the closure of the inner function, it will survive. This fact is key to simulating JavaScript's private properties, such as:
[JavaScript]View Plaincopyprint?
- function User (name,age) {
- This.setname = function (newName) {
- name = NewName; //name equivalent to SetName's Name property
- };
- This.getname = function () {
- return name;
- }
- This.getage = function () {
- return age;
- };
- This.setage = function (newage) {
- age = NewAge;
- };
- }
- var user = New User (' Ajun ', 24);
- Alert (user.getname)//ajun
- User.setname (' Lisi ', 12);
- var newName = User.getname ();
- alert (newName); //lisi
Or it can be simulated like a property, because name beyond its scope, is not accessible
[JavaScript]View Plaincopyprint?
- function user (name, age) {
- var name;
- this.getname = function () { RETURN NAME; };  
- this.setname = function (newName) {
-  NAME =  NEWNAME;   
- };
- }  
- var user = < span class= "keyword" >new user (' Ajun ', ' n ');
- user. SetName (
- alert (P.getname ());
Note: The private property that you set is not accessible by other public methods within this method (refers to a shared method, defined by User.prototype method), which is not similar to Java
Private members can only be accessed by means of having these private members within their closures
Such as: The following code, is not allowed to work
[JavaScript]View Plaincopyprint?
- user.prototype.somepublicmethod= function () {
- Alert (this.getname ());
- }
5. Namespaces
The namespace described here is equivalent to the concept of using the package in Java, which prevents the method name from conflicting and the code is as follows:
[JavaScript]View Plaincopyprint?
- Name space
- var ajun = {};
- Ajun. Examples = {};
- Ajun. examples.user=function () {
- This.setname = function (newName) {
- name = NewName;
- };
- This.getname = function () {
- return name;
- }
- This.getage = function () {
- return age;
- };
- This.setage = function (newage) {
- age = NewAge;
- };
- }
- var user = new Ajun. Examples.user ();
- User.setname (' Ajun ');
- Alert (User.getname ());
Add: In fact, with JavaScript can also implement class inheritance, etc., will use the prototype property, follow up ....
http://blog.csdn.net/ajun_studio/article/details/6837182
JavaScript OOP Ideas