JavaScript class definition and reference JavaScript advanced training Custom Objects

Source: Internet
Author: User

In the Java language, we can define our own classes and create objects from them, and in JavaScript we can also define our own classes, such as defining the user class, the Hashtable class, and so on.

One, overview

In the Java language, we can define our own classes and create objects from them, and in JavaScript we can also define our own classes, such as defining the user class, the Hashtable class, and so on.

There are already some standard classes in JavaScript, such as date, Array, RegExp, String, Math, number, and so on, which provides a lot of convenience for our programming. But for complex client programs, these are far from enough.

Unlike Java, JAVA2 provides us with a lot of standard classes that basically meet our programming needs, but JavaScript offers very few standard classes, and many programming requirements need to be implemented ourselves, such as JavaScript without Hassi Hashtable, In this case, it is inconvenient to handle the key value.

Therefore, I personally think that a complete view of JavaScript objects should look like this:

Second, the basic concept

1, custom object.
According to the JS object extension mechanism, users can customize the JS object, which is similar to the Java language.
Corresponding to the custom object is the JS standard object, such as date, Array, math, and so on.
2, prototype (prototype)
In JS, this is a way to create object properties and methods, and you can add new properties and methods to the object through prototype.
By prototype we can add new properties and methods to the JS standard object, for example, for a string object, we can add a new method trim () to it.
Unlike strict programming languages such as Java, we can dynamically add new properties to the JS object during runtime.

Third, grammatical rules

1, how objects are created

1) object initializer mode

Format: ObjectName = {property1:value1, property2:value2,..., Propertyn:valuen}
property is an attribute of an object
Value is the value of the object, which can be one of three strings, numbers, or objects
For example: Var user={name: "User1", age:18};
var user={name: "User1", Job:{salary:3000,title:programmer}
You can also initialize the method of an object in this way, for example:
var user={name: "User1", Age:18,getname:function () {
return this.name;
}
}
The following will be the focus of the constructor, including the definition of properties and methods, etc., as well as the way the constructor is explained.

2) constructor function mode

Write a constructor and create the object by using the new method, which can have the constructor parameters
For example:
function User (name,age) {
This.name=name;
This.age=age;
This.canfly=false;
}
var use=new User ();

2, defining object properties

1) JS can define three types of properties for an object: Private properties, instance properties, and class properties, like Java, where private properties can only be used inside objects, instance properties must be referenced through an instance of an object, and class properties can be referenced directly through the class name.

2) Private Property Definition
Private property can only be defined and used within the constructor.
Syntax format: var propertyname=value;
For example:
function User (age) {
            This.age=age;
           var ischild=age<12;
           This.islittlechild=ischild;
   }
    var user=new user (15);
    alert (user.islittlechild);//The correct way
    alert (user.ischild);// Error: Object does not support this property or method

3) instance attribute definitions, there are two ways:
Prototype way, syntax format: functionname.prototype.propertyname=value
This way, syntax format: this.propertyname=value, note the position used in the following example
The above value can be a character a, a number, and an object.
For example:
function User () {}
User.prototype.name= "User1";
user.prototype.age=18;
var user=new user ();
alert (user.age);
—————————————–
function User (name,age,job) {
This.name= "User1";
this.age=18;
This.job=job;
}
alert (user.age);

3) class attribute definition
Syntax format: functionname.propertyname=value
For example:
function User () {}
user.max_age=200;
user.min_age=0;
alert (user.max_age);
Refer to the class properties of the JS standard object:
Number.MAX_VALUE//Maximum value Math.PI//PI

4) for the definition of attributes, in addition to the more formal way above, there is a very special way of defining, syntax format: obj[index]=value
Example:
function User (name) {
This.name=name;
this.age=18;
This[1]= "OK";
this[200]= "Year";
}
var user=new user ("User1");
Alert (user[1]);
In the above example, note: Different through this[1] to get the age property, nor through This[0] to get the Name property, that is defined by the index method must use the index way to refer to, but not by the index method, must be referenced in the normal way

3, defining Object methods

1) JS can define three types of methods for objects: Private methods, instance methods, and class methods, similar to Java:
Private methods can only be used inside an object
The instance method must be instantiated before the object can be used
Class methods can be used directly from the class name.
Note: The definition of a method cannot be done by means of the index described above.
2) Define private methods
Private methods must be defined within the constructor body and can only be used within the constructor body.
Syntax format: function methodName (arg1,..., argN) {}
For example:
function User (name) {
This.name=name;
function Getnamelength (NAMESTR) {
return namestr.length;
}
This.namelength=getnamelength (this.name);
}
3) Define an instance method, which can now be used in two ways:
Prototype mode, used outside of the constructor, in syntax format:
Functionname.prototype.methodname=method;
Or
Functionname.prototype.methodname=function (arg1,..., argN) {};
This method, used inside the constructor, is syntactically formatted:
This.methodname=method;
Or
This.methodname=function (arg1,..., argN) {};
In the syntax described above, method is a method that already exists outside, MethodName the object to be defined by means of a method that assigns an external method directly to the object.
Defining an object method in the same way as function (Arg1,..., ArgN) {} Is what developers should know.

Some examples of defining instance methods: Example 1
function User (name) {
This.name=name;
This.getname=getusername;
This.setname=setusername;
}
function GetUserName () {
return this.name;
}
Function Setusername (name) {
This.name=name;
}

Some examples of defining instance methods: Example 2
function User (name) {
This.name=name;
This.getname=function () {
return this.name;
};
This.setname=function (newName) {
This.name=newname;
};
}

Some examples of defining instance methods: Example 3
function User (name) {
This.name=name;
}
User.prototype.getname=getusername;
User.prototype.setname=setusername ();
function GetUserName () {
return this.name;
}
Function Setusername (name) {
This.name=name;
}

Some examples of defining instance methods: Example 4
function User (name) {
This.name=name;
}
User.prototype.getname=function () {
return this.name;
};
User.prototype.setname=function (newName) {
This.name=newname;
};

4) Defining class methods
The class method needs to be defined outside the constructor and can be referenced directly by the constructor name.
Syntax format:
Functionname.methodname=method;
Or
Functionname.methodname=function (arg1,..., argN) {};
Example:
function User (name) {
This.name=name;
}
User.getmaxage=getusermaxage;
function Getusermaxage () {
return 200;
}
Or
User.getmaxage=function () {return 200;};
Alert (User.getmaxage ());

4, references to properties and methods

1) from Visibility:
Private properties and methods can only be referenced within an object.
Instance properties and methods, which can be used anywhere, but must be referenced by an object. The
class properties and methods can be used anywhere, but cannot be referenced by an instance of an object (this differs from Java in that static members in Java can be accessed through an instance).
2) from the object level:
is similar to a Java bean reference, and can be referenced in a deep sense.
Several ways:
Simple properties: Obj.propertyname
Object properties: Obj.innerObj.propertyName
Indexed properties: Obj.propertyname[index]
A deeper reference is similar to the above.
3) By definition:
A property that is defined by the index method must be referenced by index.
properties that are defined by non-index methods must be referenced in a normal way.
Note also: The method of an object cannot be defined by index.

5. Dynamic addition and deletion of attributes and methods
1) For an object that has already been instantiated, we can dynamically add and remove its properties and methods, with the following syntax (assuming the object instance is obj):
Dynamically increase object Properties
Obj.newpropertyname=value;
Dynamically increasing object methods
Obj.newmethodname=method or =function (arg1,..., argN) {}
Dynamically deleting object properties
Delete Obj.propertyname
Dynamic Delete Object Methods
Delete Obj.methodname

2) Example:
function User (name) {
This.name=name;
this.age=18;
}

JavaScript class definition and reference JavaScript advanced training Custom Objects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.