Understanding the objects in JavaScript recommend _javascript tips

Source: Internet
Author: User
There is no definition of a class in JavaScript, there are no fixed templates when creating objects, you can create new properties and methods dynamically, and when creating new properties dynamically, all we can do is create a new value for this property, and the following example is to create an object and add x,y two attributes.
Copy Code code as follows:

var programmer = new Object ();
Programmer.name = "Young";
Programmer.age = 25;
Alert (Programmer.name + ":" + programmer.age);

JavaScript objects are completely different from C # or VB objects, and JavaScript objects can be viewed as a set of set of health/value pairs, with objects. Property names to access an object property. We can think of JavaScript objects as dictionary classes in the. NET framework, and we can create object properties from the [] operator.
Copy Code code as follows:

var programmer = new Object ();
programmer["Name" = "Young";
programmer["Age"]= 25;
document.getElementById ("message"). innerhtml=programmer["Name" + ":" + programmer["age"];
Alert (Programmer.name + ":" + programmer.age); You can see from the example above that two methods of accessing an object are the same. If a property is not created, it returns "undefined".

We can also add a function as a value for a set of health/value pairs, so that the method that is constructed as an object
Copy Code code as follows:

var programmer = new Object ();
programmer["Name" = "Young";
Programmer.age= 25;
Programmer.speak=function () {
Alert (THIS.name + ":" + this["age"]);
}
Programmer.speak ();

In the code above, we alternately use the "." and [] to define accessible properties, method, which is the same as those of the two methods, sometimes these operators can cause some conceptual confusion, it is not very clear to set the new value for an already existing attribute, and we introduce the third syntax to express our intention more clearly.
Copy Code code as follows:

var programmer =
{
Name: "Young",
AGE:25,
Speak:function () {alert (this.name + ":" + This.age);}
}
Programmer.speak (); The above code more clearly expresses the beginning and end of object initialization, and we can also use this method to nest objects in the exclusive. var programmer =
{
Figure: {name: "Young", age:25},
Company: {name: "Arcadia", Address: "Shenzheng"},
Speak:function () {
Alert ("Name:" +this.) Figure.name+ "\nage:" +this. Figure.age + "\ncompany:" +this.company.name+ "

"+this.company.address);
}
};
Programmer.speak ();

This syntax is very popular because of its clear intent and compact code pattern, and you can see it in a variety of popular JavaScript frameworks, including JavaScript Object notation (JSON), which is currently popular on the internet, JSON is a lightweight data interchange format that is also easy to machine parse and generate, and is very strict in syntax. JSON allows JavaScript to exchange data on the Internet, and we can use Eval to convert a JSON object into a local JavaScript object.
Copy Code code as follows:

var programmer= "({name: ' Young ', age:25})";
var p = eval (programmer);
Alert (P.name + ', ' + p.age);

Through the above discussion, we know that all JavaScript objects are a set of dictionaries. There's another important thing in JavaScript--a function.
1: In JavaScript, a function is an object. This is completely different from the methods in C #. Let's look at an example below.
Copy Code code as follows:

function Add (point1, Point2)
{
var result = {
x:point1.x + point2.x,
Y:point1.y + point2.y
}
return result;
}
var P1 = {x:1, y:1};
var P2 = {x:2, y:2};
var p3 = Add (P1, p2);
Alert (p3.x + "," + P3.y); 2: A method that takes a function as an object. var boy=
{
Name: "Young",
Love:function (Girl) {
Return this.name+ "fell in love with" +girl.name
}
}
var girl={
Name: "Jing"
}
Alert (Boy.love (Girl));

Now the problem is two similar objects, one with a love method and one not, because we don't define classes like C #, but simply create two objects, if you expect a love method to be defined in two objects, like the following.
Copy Code code as follows:

function Loverelation (person) {
Alert (this.name+ "fell in love with" +person.name);
}
var person1={
Name: "Young",
Love:loverelation
}
var person2={
Name: "Jing",
Love:loverelation
}
Person1. Love (Person2);

The code above looks like creating a person class and then creating two instances of the man class so that the two instances have the same characteristics, apparently not enough code. There are two ways to meet this requirement.
Route 1:
Copy Code code as follows:

function person (n)
{
This.name=n;
This. Love=function (person)
{
Alert (this.name+ "fell in love with" +person.name);
}
}
var person1=new person ("young");
var person2=new person ("Jing");
Person1. Love (Person2);

Path 2: Use the prototype attribute of the Javasctipt object.
Copy Code code as follows:

function person (n)
{
This.name=n;
}
Person.prototype.love=function (person)
{
Alert (this.name+ "fell in love with" +person.name);
}
var person1=new person ("young");
var person2=new person ("Jing");
Person1. Love (Person2);
Person2. Love (Person1);

In the example above, we can look at the person function as the constructor for the person object, and all objects constructed from this constructor share a prototype property.
In Douglas Crockford's <a href= "http://www.crockford.com/javascript/private.html" >private members in JavaScript " </a>, the author gives us a detailed demonstration of how to create private members of the object, the idea is no longer explained in detail, we simply rewrite the demo
Copy Code code as follows:

function point (x, y)
{
this.get_x = function () {return x;}
this.set_x = function (value) {x = value;}
this.get_y = function () {return y;}
this.set_y = function (value) {y = value;}
}

Point.prototype.print = function ()
{
return this.get_x () + ', ' + this.get_y ();
}

var p = new Point (2,2);
P.set_x (4);
Alert (P.print ());

Finally, let's talk about the namespace of the JavaScript object, which can be used to remove conflicts between the same types, and friends who have learned C # must be very clear about this, and we can define namespaces in JavaScript with the following code. var Arcadia = {}
Copy Code code as follows:

Arcadia.person=function (n)
{
This.name=n;
}
Arcadia.person.prototype.love=function (person)
{
Alert (this.name+ "in love with" +person.name);
}
Var person1=new arcadia.person ("Young");
var person2=new arcadia.person ("Jing");
Person1. Love (Person2);
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.