javascript properties and methods

王林
Release: 2023-05-09 11:36:37
Original
548 people have browsed it

JavaScript is a widely used web programming language. Just like other programming languages, JavaScript has the concept of properties and methods. This article introduces the concepts, usage, and examples of JavaScript properties and methods.

1. Attributes

Attributes are characteristics or characteristics of an object. JavaScript objects are composed of key-value pairs, where the key is the property name and the value is the property value. Property values can be any type of data, such as strings, numbers, Boolean values, objects, arrays, etc.

  1. Declaring properties

You can use the following syntax to declare properties:

objectName.propertyName = value;
Copy after login

In this syntax, objectName is the object name, propertyName is the property name, value is the attribute value.

Here is an example:

var person = new Object(); person.age = 30; person.name = "John Doe";
Copy after login

This code creates an object named person and adds two properties to it: age and name.

  1. Access properties

You can access properties using the following syntax:

objectName.propertyName
Copy after login

Here is an example:

var person = new Object(); person.age = 30; person.name = "John Doe"; console.log(person.age); //输出:30 console.log(person.name); //输出:"John Doe"
Copy after login

This code output Two attribute values of person: age and name.

  1. Delete attributes

You can use the following syntax to delete attributes:

delete objectName.propertyName;
Copy after login

Here is an example:

var person = new Object(); person.age = 30; person.name = "John Doe"; delete person.age; console.log(person.age); //输出:undefined console.log(person.name); //输出:"John Doe"
Copy after login

This code deletes The age attribute of the person object.

2. Methods

Methods are operations that an object can perform. Methods are functions of an object. Methods are typically used to modify the state of an object or perform some action.

  1. Declaring methods

Methods can be declared through the following syntax:

objectName.methodName = function() { //方法代码 };
Copy after login

In this syntax, objectName is the object name and methodName is the method name. Method code is function code defined within curly braces ({}).

Here is an example:

var person = new Object(); person.age = 30; person.getName = function() { return "John Doe"; }
Copy after login

This code creates an object named person and adds a method getName to it.

  1. Calling methods

You can use the following syntax to call methods:

objectName.methodName();
Copy after login

Here is an example:

var person = new Object(); person.age = 30; person.getName = function() { return "John Doe"; } console.log(person.getName()); //输出:"John Doe"
Copy after login

This code calls The getName method of the person object is used, and the value returned by the method is output.

  1. Delete method

You can use the delete keyword to delete an object:

delete objectName.methodName;
Copy after login

Here is an example:

var person = new Object(); person.age = 30; person.getName = function() { return "John Doe"; } delete person.getName; console.log(person.getName()); //输出:undefined
Copy after login

This The code snippet deletes the getName method of the person object.

3. Summary

JavaScript properties and methods are very important and basic concepts in Web programming and can be used to represent the characteristics and operations of objects. It's important to understand and become familiar with these concepts when writing JavaScript code. Through the above examples, you should have mastered the usage and syntax of properties and methods. I hope this article will be helpful to you.

The above is the detailed content of javascript properties and methods. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!