Reference Type in Javascript)

Source: Internet
Author: User

First, let's take a look at a javascript interview question.Source code(I made a small change). This example is used to understand the reference type in JavaScript. Then we made a preliminary study on the objects in JavaScript.

 
1:VaR A = {X: 1 };
 
2:VaR test = function (OBJ)
 
3:{
 
4:OBJ. x = 2;
 
5:Console. Log ("Set obj. x = 2, A. X value is"+ A. X );
 
6:A. X = 5;
 
7:Console. Log ("Set A. X = 5, obj. x value is"+ Obj. X );
 
8:OBJ = {X: 3 };
9:Console. Log ("Set OBJ = {X: 3}; A. X value is"+ A. X );
 
10:Console. Log ("Set OBJ = {X: 3}; obj. x value is"+ Obj. X );
 
11:}
 
12:Test ();
 
13:Console. Log (A. X); // 5

 

"Everything" in Javascript is an object: a string, a number, an array, a date ....

1. In JavaScript, an object is data, with properties and methods.

1.1 properties are values associated with an object.

1.2 methods are actions that can be stored med on objects.

1.3 accessing objects properties and methods (Access Object Attributes and methods)

 
1:Objectname. propertyname
 
2: 
 
3:Objectname. methodname ()

2. Creating a direct instance (create a direct instance)

2.1 define objects

The so-called "direct" means that JavaScript does not need to declare and add the property wrapper just like CSHARP and Java, and uses objectname. propertyname = propertyvalue to directly add and assign values.

 
1:Person =NewObject ();
 
2:Person. firstname ="John";
 
3:Person. lastname ="Doe";
4:Person. Age = 50;
 
5:Person. eyecolor ="Blue";

 

The syntax equivalent of the preceding definition is equivalent to the following syntax (using object literals ):

1:Person = {firstname:"John", Lastname:"Doe", Age: 50, eyecolor:"Blue"};

2.2 Use constructors

When constructing an object set, you can use constructors to quickly obtain instances of more objects.

1:Function person (firstname, lastname, age, eyecolor)
 
2:{
 
3:This. Firstname = firstname;
 
4:This. Lastname = lastname;
 
5:This. Age = age;
 
6:This. Eyecolor = eyecolor;
 
7:}
 
8:// Creating instance of person
9:VaR myfather =NewPerson ("John","Doe", 50,"Blue");
 
10:VaR mymother =NewPerson ("Sally","Rally", 48,"Green");

2.3 add methods to JavaScript objects

 
1:Function person (firstname, lastname, age, eyecolor)
 
2:{
 
3:This. Firstname = firstname;
4:This. Lastname = lastname;
 
5:This. Age = age;
 
6:This. Eyecolor = eyecolor;
 
7:// Adding mthods to person object
 
8:This. Changename = changename;
 
9:Function changename (name)
 
10:{
 
11:This. Lastname = Name;
 
12:}
 
13:}

3. Traverse JavaScript objects through Properties

"The JavaScript For... in statement loops through the properties of an object ."

 
1:VaRPerson = {fname:"John", Lname:"Doe", Age: 25 };
 
2:VaRTXT ="";
 
3:For(XInPerson)
 
4:{
 
5:TXT = TXT +""+ Person [x];
 
6:}
 
7:Console. Log ("Person info :"+ Txt );

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.