In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to build objects and custom objects in JavaScript, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
JavaScript object
Everything in JavaScript is an object: strings, numeric values, arrays, functions.
In addition, JavaScript allows custom objects.
JavaScript provides multiple built-in objects, such as String, Date, Array, and so on.
Objects are just special data types with properties and methods.
Access the properties of an object
Property is the value associated with the object. The syntax for accessing object properties is:
ObjectName.propertyName
This example uses the length property of the String object to get the length of the string:
Var message= "Hello World!"; var x=message.length
Note: after the above code is executed, the value of x will be: 12.
The method of accessing the object
A method is an action that can be performed on an object. You can call a method with the following syntax:
ObjectName.methodName ()
This example uses the toUpperCase () method of the String object to convert the text to uppercase:
Var message= "Hello world!"; var x=message.toUpperCase ()
After the above code is executed, the value of x will be: HELLO WORLD!.
Create a JavaScript object
With JavaScript, you can define and create your own objects. There are two different ways to create a new object:
Define and create an instance of the object.
Use functions to define objects, and then create new object instances.
Create an instance of an object
This example creates a new instance of the object and adds four properties to it:
Person=new Object (); person.firstname= "Bill"; person.lastname= "Gates"; person.age=56; person.eyecolor= "blue"; [xss_clean] (person.firstname + "is" + person.age + "years old.")
Alternative syntax (using object literals):
Person= {firstname: "Bill", lastname: "gates", age:56,eyecolor: "blue"} [xss_clean] (person.firstname + "is" + person.age + "years old.")
Use the object constructor
This example uses a function to construct an object:
Function person (firstname,lastname,age,eyecolor) {this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor;} myFather=new person ("Bill", "Gates", 56, "blue"); [xss_clean] (myFather.firstname + "is" + myFather.age + "years old.")
Create an instance of a JavaScript object
Once you have an object constructor, you can create a new object instance, like this:
Var myFather=new person ("Bill", "Gates", 56, "blue"); var myMother=new person ("Steve", "Jobs", 48, "green")
Add attributes to the JavaScript object
You can add new properties to existing objects by assigning values to them.
Assuming that personObj already exists-you can add new properties to it: firstname, lastname, age, and eyecolor:
Person.firstname= "Bill"; person.lastname= "Gates"; person.age=56;person.eyecolor= "blue"; x=person.firstname
Note: after the above code is executed, the value of x will be: Bill.
Add a method to the JavaScript object
A method is simply a function attached to an object. The way to define an object inside a constructor function:
Function person (firstname,lastname,age,eyecolor) {this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; this.changeName=changeName; function changeName (name) {this.lastname=name;}} myMother=new person ("Steve", "Jobs", 56, "green"); myMother.changeName ("Ballmer"); [xss_clean] (myMother.lastname)
Note: the value of the changeName () function name is assigned to the lastname attribute of person.
JavaScript class
JavaScript is an object-oriented language, but JavaScript does not use classes.
In JavaScript, classes are not created, and objects are not created through classes (as in other object-oriented languages).
JavaScript is based on prototype, not class.
JavaScript for...in cycle
The JavaScript for...in statement loops through the properties of the object.
Click the button below to loop through the properties of the object "person".
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.