JavaScript Functions (6)

Source: Internet
Author: User

Add attribute

When we use the keyword new to create an object as above, a new object is created. We can add attributes to this object after the object is created (as if I have added the attribute name as above. The next problem is that if we create another instance of this object, we have to add this attribute to this new object as follows .)

Example dt3 (creates 3 ball objects)


Function ball ()
VaR ball0 = new ball (); // ball0 now points to a new instance of the type ball = "ball-0"; // ball0 now has an attribute "name"

VaR ball1 = new ball (); = "ball-1 ";

VaR ball2 = new ball ();

Alert (; // output "ball-0"
Alert (; // output "ball-1"
Alert (; // Oh, I forgot to add "name" to ball2!

I forgot to add the attribute name to ball2. If it is in a formal program, this may cause problems. Is there any good way to automatically add attributes? Well, there is one: Use the this keyword. This word has special significance in function. It points to the object that calls the function. Let's take a look at another example below. At this time, we add these attributes to the constructor:

Example dt4

Function ball (message, specifiedname)
Alert (Message );
This. Name = specifiedname;
VaR ball0 = new ball ("creating new ball", "soccer ball ");
Alert (; // prints "soccer ball"

Remember: The New Keyword eventually executes the constructor. In this example, it will run ball ("creating new ball", "soccer ball"), and the keyword this will point to ball0.
Therefore, this line: This. Name = specifiedname is changed to = "soccer ball ".
It mainly refers to adding the attribute name to ball0 and the attribute value is soccer ball.
Now we just added a name attribute to ball0, which looks like what we did in the previous example, but it is a better and more scalable method. Now, we can create many balls with attributes as we like without manually adding them. In addition, people also hope that the ball object can clearly understand its constructor and easily find all the attributes of the ball. Let's add more attributes to ball.

Example dt5


Function ball (color, specifiedname, owner, weight)
This. Name = specifiedname;
This. Color = color;
This. Owner = owner;
This. Weight = weigth;
VaR ball0 = new ball ("black/white", "soccer ball", "John", 20 );
VaR ball1 = new ball ("gray", "bowling ball", "John", 30 );
VaR ball2 = new ball ("yellow", "Golf Ball", "John", 55 );
VaR balloon = new ball ("red", "balloon", "Pete", 10 );

Alert (; // output "soccer ball"
Alert (balloon. Name); // output "balloon"
Alert (ball2.weight); // output "55"

Hey! Using object-oriented terminology, you can say that ball is an object type with the following attributes: name, color, owner, and weight.

Assign an object to a property

We are not limited to adding simple data types such as strings or numbers as attributes. We can also assign objects to attributes. The supervisor is an attribute of the employee.

Example dt6


Function employee (name, salary, mysupervisor)
This. Name = Name;
This. Salary = salary;
This. Supervisor = mysupervisor;
VaR boss = new employee ("John", 200 );

VaR manager = new employee ("Joan", 50, boss );
VaR teamleader = new employee ("Rose", 50, boss );

Alert (Manager. Supervisor. Name + "is the supervisor of" + manager. Name );
Alert (Manager. Name + "\'s supervisor is" + manager. Supervisor. Name );


What will be output?

As you can see in the preceding example, both Manager and teamleader have a supervisor attribute, which is an object of the type of employee.

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: 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.