Methods for setting up objects in JS _ basic knowledge
Source: Internet
Author: User
JS object creation method Objects are useful to organize information.
Objects are very useful for organization information.
JavaScript Objects
JS object
Earlier in this tutorial we have seen that JavaScript has several built-in objects, like String, Date, Array, and more. in addition to these built-in objects, you can also create your own.
In the previous sections of the tutorial, we have seen that JS has some built-in objects, such as String, Date, Array, and more. In addition, we can create our own objects.
An object is just a special kind of data, with a collection of properties and methods.
Objects are special data and have a series of related attributes and methods.
Let's modify strate with an example: A person is an object. properties are the values associated with the object. the persons 'properties include name, height, weight, age, skin tone, eye color, etc. all persons have these properties, but the values of those properties will differ from person to person. objects also have methods. methods are the actions that can be stored med on objects. the persons 'Methods cocould be eat (), sleep (), work (), play (), etc.
Let's illustrate an example: a person is an object. Attribute is the value associated with the object. A person's attributes include name, height, weight, age, skin color, and eye color. All people have these attributes, but their values may be different from those of others. The object also has methods. A method is the action of an object. The human method can be eat () [eat], sleep () [sleep], work () [work], and so on.
Properties
The syntax for accessing a property of an object is:
The attribute syntax for associating an object is:
ObjName. propName
You can add properties to an object by simply giving it a value. Assume that the personObj already exists-you can give it properties named firstname, lastname, age, and eyecolor as follows:
You can add attributes to an object by assigning values. Assuming that personObj already exists-you can add the last name and name of the object and the following age and eye color:
Creating Your Own Objects
Create your own object
There are different ways to create a new object:
There are two different ways to create a new object
1. Create a direct instance of an object
Directly create
The following code creates an instance of an object and adds four properties to it:
The following code creates an object and adds four attributes to it:
PersonObj = new Object ()
PersonObj. firstname = "John"
Adding a method to the personObj is also simple. The following code adds a method called eat () to the personObj:
Creating a method for an object is also very simple. The following code adds an eat () method
PersonObj. eat = eat
2. Create a template of an object
Create an object module
The template defines the structure of an object:
Framework of the module-Defined Object
Function person (firstname, lastname, age, eyecolor)
{
This. firstname = firstname
This. lastname = lastname
This. age = age
This. eyecolor = eyecolor
}
Notice that the template is just a function. inside the function you need to assign things to this. propertyName. the reason for all the "this" stuff in is that you're going to have more than one person at a time (which person you're dealing with must be clear ). that's what "this" is: the instance of the object at hand.
Note that the module is just a function, and you need to allocate something to this. propertyName in the function. All are "this" because you will suddenly have more than one person (which person you must know ).
Once you have the template, you can create new instances of the object, like this:
Once you have a module, you can directly create a new object like this:
MyFather = new person ("John", "Doe", 50, "blue ")
MyMother = new person ("Sally", "Rally", 48, "green ")
You can also add some methods to the person object. This is also done inside the template:
You can also add some methods to the person object, which can also be completed in the module:
Function person (firstname, lastname, age, eyecolor)
{
This. firstname = firstname
This. lastname = lastname
This. age = age
This. eyecolor = eyecolorthis. newlastname = newlastname
}
Note that methods are just functions attached to objects. Then we will have to write the newlastname () function:
Note: This method is only an additional function of the object. Next we must write the newlastname () function.
Function newlastname (new_lastname)
{
This. lastname = new_lastname
}
The newlastname () function defines the person's new last name and assigns that to the person. javaScript knows which person you're talking about by using "this. ". so, now you can write: myMother. newlastname ("Doe ").
The newlastname () function defines the new last name of the person and assigns it to the person. If you use "this", JS will understand the person you are describing. So now you can write: myMother. newlastname ("Doe ")
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.