Home Web Front-end JS Tutorial Javascript object-oriented Object (Object)_js object-oriented

Javascript object-oriented Object (Object)_js object-oriented

May 16, 2016 pm 06:27 PM
object object object-oriented

Object creation statement in javascript:
var obj = {}; or var obj = new Object();
Add attributes to the object, method:
//=====First way of writing= ===================================
obj.name = 'Xiao Ming'; // is an object Add attributes
obj.updateName = function(name){//Define the updateName method for the object
this.name = name;
}
alert(obj.name);
obj.updateName ("Xiaoqiang"); //Call updateName to modify the name attribute value of the obj object
alert(obj['name']);
The result displayed for the first time is: Xiao Ming
The result displayed for the second time is :Xiaoqiang
//======The second way of writing==================================== ==
obj['name'] = 'Zhang San'; //Add attributes to the object
obj['updateName'] = function(name){//Define the updateName method for the object
obj[ 'name'] =name;
};
alert(obj.name);
obj.updateName('李思'); //Call updateName to modify the name attribute value of the obj object
alert (obj['name']);
The first time the result is displayed: Zhang San
The second time the result is displayed: Li Si

Copy code The code is as follows:

//======The third way of writing================== ==================
var obj = {
name: '王五', //Add attributes to the object
updateName: function(name) {//Define the updateName method for the object
this.name = name;  
}
};
alert(obj .name);
obj.updateName("Zhao Liu"); / /Call updateName to modify the name attribute value of the obj object
alert(obj .name);

The first time the result is displayed: Wang Wu
The second time the result is: Zhao Liu
//=====Analysis========================================
The first way of writing is the most common way of writing objects, because JavaScript is a dynamic language, different from Java and .Net.
After the program runs and creates the object, the internal structure of the object can also be modified,
For example, adding properties and methods (the reflection mechanism in java and .net cannot do this).
(a): var obj = {} || new Object();
(b): obj.name = "Zhang San";
(c): obj.updateName = function(name) { this.name = name};
When the program executes (a), an empty object (does not contain any methods and properties) obj is created.
When the program executes (b), the object of obj is changed. The internal structure adds an attribute name.
When the program executes (c), the internal structure of obj is changed and a method updateName is added.
These are all actions completed during running time
The second way of writing is like an array, but it is definitely not an array. To distinguish whether it is an array or not, you can judge it like this:
Copy the code The code is as follows:

if(typeof(obj.length) == "undefined") {
alert("obj is not an array, arrays have the length attribute!");
}else{
alert("obj is an array!");
}

The second way of writing is more like a data structure: map, such as: obj[key] = value;
key Is a string, value can be any type, variable, object, function, etc.
You can traverse the internal structure of the object in this way:
Copy code The code is as follows:

for(var key in obj)
{
alert(key);
var value = obj[key];
alert(value);
}

Alert can display the content you define.
The third way of writing is the internal structure of map at first glance. An object is completely represented internally by key:value pairs.
JSON objects also have this structure. As long as you are familiar with map or JSON objects It's easy to understand.
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1242
24
How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

PHP Advanced Features: Best Practices in Object-Oriented Programming PHP Advanced Features: Best Practices in Object-Oriented Programming Jun 05, 2024 pm 09:39 PM

OOP best practices in PHP include naming conventions, interfaces and abstract classes, inheritance and polymorphism, and dependency injection. Practical cases include: using warehouse mode to manage data and using strategy mode to implement sorting.

Explore object-oriented programming in Go Explore object-oriented programming in Go Apr 04, 2024 am 10:39 AM

Go language supports object-oriented programming through type definition and method association. It does not support traditional inheritance, but is implemented through composition. Interfaces provide consistency between types and allow abstract methods to be defined. Practical cases show how to use OOP to manage customer information, including creating, obtaining, updating and deleting customer operations.

How do PHP functions return objects? How do PHP functions return objects? Apr 10, 2024 pm 03:18 PM

PHP functions can encapsulate data into a custom structure by returning an object using a return statement followed by an object instance. Syntax: functionget_object():object{}. This allows creating objects with custom properties and methods and processing data in the form of objects.

Are there any class-like object-oriented features in Golang? Are there any class-like object-oriented features in Golang? Mar 19, 2024 pm 02:51 PM

There is no concept of a class in the traditional sense in Golang (Go language), but it provides a data type called a structure, through which object-oriented features similar to classes can be achieved. In this article, we'll explain how to use structures to implement object-oriented features and provide concrete code examples. Definition and use of structures First, let's take a look at the definition and use of structures. In Golang, structures can be defined through the type keyword and then used where needed. Structures can contain attributes

Analysis of object-oriented features of Go language Analysis of object-oriented features of Go language Apr 04, 2024 am 11:18 AM

The Go language supports object-oriented programming, defining objects through structs, defining methods using pointer receivers, and implementing polymorphism through interfaces. The object-oriented features provide code reuse, maintainability and encapsulation in the Go language, but there are also limitations such as the lack of traditional concepts of classes and inheritance and method signature casts.

What should I pay attention to when a C++ function returns an object? What should I pay attention to when a C++ function returns an object? Apr 19, 2024 pm 12:15 PM

In C++, there are three points to note when a function returns an object: The life cycle of the object is managed by the caller to prevent memory leaks. Avoid dangling pointers and ensure the object remains valid after the function returns by dynamically allocating memory or returning the object itself. The compiler may optimize copy generation of the returned object to improve performance, but if the object is passed by value semantics, no copy generation is required.

Analyze the differences between heap and stack in Java and their application scenarios Analyze the differences between heap and stack in Java and their application scenarios Feb 24, 2024 pm 11:12 PM

The difference between Java heap and stack and application scenario analysis require specific code examples. In Java programs, heap and stack are two commonly used data structures, and they assume different roles and functions in memory. Understanding the difference between heap and stack is crucial to writing efficient Java programs. First, let's take a look at the Java heap. The heap is an area used to store objects. All objects created in the program are stored in the heap. The heap is where memory is dynamically allocated and released while the program is running. It is not subject to any restrictions and can be automatically allocated and released as needed.

See all articles