Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create JavaScript access object method

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to create JavaScript access object method". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to create JavaScript access object method".

JavaScript access object method

Use the following syntax to create an object method:

MethodName: function () {Line of Code}

Access the object method with the following syntax:

ObjectName.methodName ()

You would usually describe fullName () as a method of a person object and fullName as a property.

The fullName property is executed as a function after it is called by ().

This example accesses the fullName () method of the person object:

Example

Name = person.fullName ()

If you do not use () when you access the fullName property, the function definition is returned:

Example

Name = person.fullName

Use the built-in method

This example uses the toUpperCase () method of the String object to convert the text to uppercase:

Var message = "Hello world!"

Var x = message.toUpperCase ()

The value of x, after the above code is executed, will be:

HELLO WORLD!

Add a new method

Adding a method to an object is done inside the constructor function:

Example

Function person (firstName, lastName, age, eyeColor) {

This.firstName = firstName

This.lastName = lastName

This.age = age

This.eyeColor = eyeColor

This.changeName = function (name) {

This.lastName = name

}

}

The value of the changeName () function name is assigned to the lastName attribute of person.

Now you can try:

MyMother.changeName ("Jobs")

Make it clear which person you are referring to by "replacing" this,JavaScript with myMother.

Thank you for your reading, the above is the content of "how to create JavaScript access object methods". After the study of this article, I believe you have a deeper understanding of how to create JavaScript access object methods, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report