JS Custom Object

Source: Internet
Author: User

JS Creating Classes and Objects (1)

JavaScript Create Class / objects in several ways

in js create object ) not exactly what we always say. Create a Class object, js " emphasizes a compound type, js

JS An object is a composite type that allows you to store and access through variable names, in other thoughts, an unordered collection of attributes, each of which consists of a name and a value (sounds like something we often hear. HASH tables, dictionaries, health / value pairs? ), where the value type may be a built-in type ( such as number,string), or it may be an object.

A. Enclosed by a pair of curly braces

    varemptyobj = {};
varMYOBJ =
{
' ID ': 1,
//attribute names are enclosed in quotation marks, separated by commas between attributes.
' Name ': ' MyName '
};
//var m = new MYOBJ ();//not supported

  I don't know if you noticed that the objects were all used var declared no, like the above code, just simply declare an object, it has only one copy, you can not like the instantiation of the class object to use it New action, like the comment section of the above code. This greatly restricts the reuse of objects, unless you create objects that require only one copy, or else consider building objects in other ways.

Here's a look at how to access the properties and methods of an object.

    varMYOBJ =
{
' ID ': 1,
' Fun ':
function() {
Document.writeln (
This. ID + '-' + This. Name);//in"Object.Properties"Way Access
},
' Name ': ' MYOBJ ',
' Fun1 ':
function() {
Document.writeln (
This[' id '] + ' + ' + This[' name ']);//access in a collective way
        }
};
Myobj.fun ();
Myobj.fun1 ();
// Results
    //1-myobj 1+myobj

second, with function keyword Simulation class

in the function in use This refers to the current object, declaring the property by assigning a value to the property. If a variable is declared with var , the variable is a local variable and is only allowed in the class definition.

        functionMyClass () {
This. id = 5;
This. Name = ' MyClass ';
This. GetName =function() {
return This. Name;
}
}
varmy =NewMyClass ();
alert (my.id);
Alert (My.getname ());
// Results
        //5
        //MyClass

Create an object in the function body, declaring its properties and returning

creating an object in the body of a function can take advantage of the 1th method, or first new Object (); assign values to each property.

However, objects created in this way are VS2008 SP1 There is no smart hint in the.

        functionMyClass () {
varobj =
{
' ID ': 2,
' Name ': ' MyClass '
};
returnobj;
}
function_myclass () {
varobj =NewObject ();
Obj.id = 1;
Obj.name = ' _myclass ';
returnObj
}
varmy =NewMyClass ();
var_my =New_myclass ();
alert (my.id);
alert (my.name);
alert (_my.id);
alert (_my.name);

// Results
        //2
        //MyClass
        //1
        //_myclass

JS Custom Object

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.