Creating objects (Create Object) __javascript programming notes

Source: Internet
Author: User
Tags class definition

In JS, creating objects (create object) is not exactly what we often say to create class objects, JS objects are emphasized is a composite type, JS to create objects and access to objects is extremely flexible.

JS object is a composite type, which allows you to store and access through variable names, in another way, an object is a unordered set of attributes, each of which is made up of names and values (sounds like a hash table, dictionary, health/value pair that we often hear about.) , where the value type may be a built-in type (such as number,string) or an object.

enclosed by a pair of curly bracesvar emptyobj = {};
var myobj =
{
' ID ': 1,//property name enclosed in quotes, attributes separated by commas
' Name ': ' MyName '
};
var m = new MyObj (); Not supported I don't know if you noticed that objects are declared with Var, like the above code, just simply declare an object, it has only one copy, you can't use the new operation like the instantiation class object, like the annotation part of the above code.   This greatly restricts the reuse of objects, unless you create objects that require only one copy, otherwise consider creating objects in other ways.      Let's look at how to access the object's properties and methods. var myobj =
{
' ID ': 1,
' Fun ': function () {
Document.writeln (this. ID + '-' + this. name); Access with the "object. Properties" method
},
' Name ': ' MyObj ',
' Fun1 ': function () {
Document.writeln (this [' id '] + ' + ' + ' + ' + ' [' name ']]; Access by collection
}
};
Myobj.fun ();
Myobj.fun1 ();
Results
1-myobj 1+myobjsecond, using function keyword simulation classUse this in function to refer to the current object, declaring the property by assigning it to the property.          If you declare a variable with VAR, the variable is a local variable and is only allowed to be called in the class definition. function MyClass () {
this. id = 5;
this. Name = ' MyClass ';
this. GetName = function () {
return this. Name;
}
}
var my = new MyClass ();
alert (my.id);
Alert (My.getname ());
Results
5
MyClasscreate an object in the function body, declare its properties, and returnCreating an object in a function body can take advantage of the 1th method, or the new object () first; Assign values to each property.          However, objects created in this way do not have a smart hint in the VS2008 SP1. function MyClass () {
var obj =
{
' ID ': 2,
' Name ': ' MyClass '
};
return obj;
}
function _myclass () {
var obj = new Object ();
Obj.id = 1;
Obj.name = ' _myclass ';
return obj;
}
var my = new MyClass ();
var _my = new _myclass ();
alert (my.id);
alert (my.name);
alert (_my.id);
alert (_my.name);

Results
2
MyClass
1
_myclass Category: JavaScript

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.