Object-oriented introduction in JavaScript _ js object-oriented

Source: Internet
Author: User
In JavaScript, we can say that everythingisobject, so what is an object? An object is a set of variables and functions. In other object-oriented languages, objects are instantiated by classes. JavaScript is a prototype-based object-oriented language without the concept of classes. objects are derived from existing object copy objects.
Create
Object
Constructor
Public, private, privileged, static member
This, call, and apply
Exception Handling
Inheritance
Prototype
Object
In JavaScript, we can say that everything is object. So what is an object? An object is a set of variables and functions. In other object-oriented languages, objects are instantiated from classes. JavaScript is a prototype-based object-oriented language without the concept of classes. objects are derived from copies of existing objects. Objects in JavaScript can be divided into two types: Function and Object.

Create object

To improve efficiency, JavaScript comes with built-in objects, such as objects, functions, and arrays. All built-in objects can be created using new. Function objects are divided into two types: instance and constructor. For example, alert ('My name is X') is a Function instance. The Function used as the constructor must be instantiated through new. The syntax of the created object is as follows:

The Code is as follows:


Var obj = new Object (); var obj = {}; (similar to Array)
Var myFunction = new Function () {// code}; function myFunction () {// code}


Note that the first Function declaration must be used before use, and the second Function can be used after use.

Constructor

Function is the starting point of the constructor. Creating a constructor is similar to creating an object Function.

The Code is as follows:


Var myFunction = new Function ('A',/* Code */)
Function myFunction (){
/* Code */
}


However, the second type is recommended because of the first type of performance problems. The feature of a Function object is that its instance can also be used as a constructor.

Static Member

The following code:

The Code is as follows:


Var myObj = new Object ();
// Add the name attribute
MyObj. name = 'ld ';
// Add the alertName Method
MyObj. alertName = function (){
Alert (this. name );
}
// Execute alertName
MyObj. alertName ();


Name and alertName exist only in the myObj instance and do not exist in the constructor. This is easy to understand, but it is not easy to understand for functions that can be constructor or instance, as shown below:

The Code is as follows:


Var myConstructor = new function (){
// Add static attributes
MyConstructor. name = 'ld ';
// Add a static method
MyConstructor. alertName = function (){
Alert (this. nam );
}
}
MyConstructor. alertName ();


The code can run normally, because myConstructor can be an instance, but name and alertName will not be applied to any new instance of myConstructor.

Public Member

Members that can be instantiated with objects are called public members. To become public members, you need to modify the prototype of the function. The public method can be inherited along with the constructor. The method is as follows:

The Code is as follows:


Function myConstructor (){
}
// Add public attributes
MyConstructor. prototype. myName = 'ld ';
// Instantiate
Var myObj = new myConstructor ();
Alert (myObj. myName );


Objects instantiated by myConstructor can use myName, but myConstructor itself cannot, because we add public members to the underlying definition of myConstructor, rather than the myConstructor instance itself.
Private member
Private Members refer to the variables and methods defined in constructors. They are similar to private definitions in classes in other languages. For example:

The Code is as follows:


Function myConstructor (){
// Add private attributes
Var myName = 'ld 'l
// Add a private Method
Var alertName = function (){
Alert ('ld ');
}
AlertName ();



Privileged Member

A privileged method is a method that can be publicly accessed and can access private members. The method defined by this is used in the constructor scope, similar to the public method in other languages, is as follows:

The Code is as follows:


Function myConstructor (){
// Private attributes
Var sex = 'male ';
// Privileged method
This. alertSex = function (){
Alert (sex );
}
}



Object literal

Previously, we used Dot creation, for example, myConstructor. name = x; myConstructor. sex = x. We can also use the object literal to achieve the same purpose, for example:

The Code is as follows:


Function myConstructor (){
}
// Add a Public Member
MyConstructor. prototype = {
Name: 'ld ',
Sex: 'male ',
Method: function (){}
}


Note that the Separator in the object literal is comma and there is no comma at the end of the last attribute or method to prevent parsing errors.
This, call, and apply
This is a keyword that depends on the execution environment and has nothing to do with the creation location. this keyword points to the object that uses the function that contains it. It has learned C ++ and other languages, this is not hard to understand.
Call and applay. These two functions force the method to be attached to an object, for example:

The Code is as follows:


// AlertName is a created function.
// When alertName does not require a parameter
AlertName. call ('object ')
// When alertName requires a parameter
AlertName. call ('object', 'parameter 1', 'parameter 2 ')
// When alertName uses the parameter Array
AlertName. applay ('object', 'parameter array arguments ')



Exception Handling

Similar to c #, it consists of try and catch, as shown below:

The Code is as follows:


Function myFunction (){
Window. style. color = 'red ';
}
Try {
MyFunction ();
}
Catch {
Alert ('exception message: '+ exception. name + exception. message)
}


There are many inheritance and prototype, which will be added to the next blog "inheritance and prototype in JavaScript".
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.