JS creates classes and objects in two different ways. js is different.

Source: Internet
Author: User

JS creates classes and objects in two different ways. js is different.

In JavaScript, When you define a new function, you actually declare a new class, and the function itself is equivalent to the class constructor. The following code shows you two different ways to create a new Person class, and the definition of Person. prototype follows the definition of the function.

Var Person = function (name) {// an anonymous function, which is assigned to a Person variable. At this time, Person becomes a class this. name = name;} function Person (name) {// directly defines a function called Person to represent the Person class this. name = name;} Person. prototype = {// define the prototype field printName: function () {// define a print function alert (this. name );}}

After you declare a class through the function method, You Can instantiate this class through the new operator. In this way, you can call the member functions of the class to complete your logic.

Var person = new Person ("Joe Smith"); // use the new operator to create an instance of Person and assign it to the variable "person. printName (); // person can be considered as a reference of an instance, so you can call the member function in the Person class through this reference.

Let's summarize the entire process and steps for creating a new class instance:

1. Declare a new class by defining a function (anonymous or real-name.
2. If necessary, define the prototype field of the new class.
3. Use the new operator to keep up with the function you define to create an instance of a new class. Once the JavaScript compiler encounters the new operator, it actually creates an empty class instance variable.
4. Copy all attributes and methods in the prototype field of the class to the new instance, and point all this pointers in the member functions to the newly created instance.
5. Next, execute the function that follows the new operator.
6. When you execute this function, if you try to assign a value to a non-existent attribute, the JavaScript compiler will automatically create this attribute for you within the scope of this instance.
7. After the function is executed, return the instance after initialization.

In Prototype, using a Class object, you can declare a new object in a simple way. By using Class. create (), prototype creates a default constructor initialize () for you. Once you implement this function, you can create an instance of a new class in a way similar to the constructor in Java.


How to create an object class object in JS? I want to upload an object class object to a method of action.

Business. ResultType = Ext. extend (Ext. form. ComboBox ,{
Editable: false,
TypeAhead: true,
Mode: 'local ',
Readonly: true,
TriggerAction: 'all ',
SelectOnFocus: true,
Plugins: new Ext. ux. plugin. TriggerfieldTooltip (),
Tooltip :{
Title: 'result type ',
Text: 'select the result type'
},
ValueField: 'id ',
DisplayField: 'name ',

InitComponent: function (){
This. store = new Ext. data. SimpleStore ({
Fields: ['id', 'name'],
Data :[
['10', '1],
['20', '2'],
['30', 'source'],
['40', 'access'],
['50', 'xiang'],
['60', 'already ']
]
});
Business. ResultType. superclass. initComponent. call (this );

}
});
 
In JS, which method is the most suitable for creating an object (no matter what type?

Var object;
Object = new Object ();
Object = 1;
Object = "1 ";
Object = new array ();
Object = {};
Object = [];
Object = function (){};

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.