Home Web Front-end JS Tutorial JavaScript object-oriented - creation of simple objects and JSON objects

JavaScript object-oriented - creation of simple objects and JSON objects

Jan 19, 2017 pm 03:03 PM

JavaScript is an object-based programming language, and its essence is actually object-oriented. The characteristic of object-oriented languages ​​is that they all have the concept of classes, through which any number of objects with the same properties and methods can be created. But there is no concept of class in JavaScript. Objects in JavaScript are usually called prototype objects. We can create objects directly through Object. For example, the following code:

1

2

3

4

5

6

var person = new Object();

person.name = "张三";

person.age = 20;

person.say = function(){

  alert(this.name+","+this.age);

}

Copy after login

In the above code, a person object is simply created through Object, and then 2 properties and 1 method are set for the person object.

The biggest problem caused by the objects created in the above way is that there is no class constraint, so the object cannot be reused, and there is no agreement, which will cause problems in operation.

Json object

We cannot transmit a JavaScript object over the network, only strings can be transmitted over the network. A feasible method of transmitting objects is to write the objects in XML format for transmission, for example:

1

2

3

4

5

6

<?xml version="1.0" encoding="utf-8"?>

<preson>

  <id>1</id>

  <name>张三</name>

  <age>20</age>

</person>

Copy after login

However, when using XML format for data transmission, a large number of additional tag strings will be generated during the transmission process, so The transmission efficiency is obviously not high. In order to solve these problems, people have developed another string object format: Json object format.

The full name of Json is javascript simple object notation, which is a simple data exchange format. The Json object is a JavaScript object, but it omits the tags in xml and uses {} to complete the description of the object.

The Json format defines attributes through attribute name: attribute value. Different attributes are separated by commas (,). The last attribute does not need to add a comma. For example, the following code defines a person object in Json format.

1

2

3

4

5

6

7

var person = {

  name : "张三",

  age:23,

  say:fuction(){

    alert(this.name+","+this.age);

  }

}

Copy after login

The properties and methods to call the person object are the same as those used by ordinary JavaScript objects, for example:

1

2

// 调用person的say方法

person.say();

Copy after login

We can also create object arrays through Json, and the creation method is the same as JavaScript arrays. Same.

1

2

3

4

var ps = [

  {name:"Leon",age:22},

  {name:"Ada",25}

];

Copy after login

After completing the creation of the array, we can also traverse the Json objects in the array.

1

2

3

for(var i = 0; i < ps.length; i++){

  alert(ps[i].name);

}

Copy after login

The above is JavaScript object-oriented - the creation of simple objects and the content of JSON objects. For more related content, please pay attention to the PHP Chinese website (www.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

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)

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

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.

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.

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.

Quick tips for converting PHP arrays to JSON Quick tips for converting PHP arrays to JSON May 03, 2024 pm 06:33 PM

PHP arrays can be converted to JSON strings through the json_encode() function (for example: $json=json_encode($array);), and conversely, the json_decode() function can be used to convert from JSON to arrays ($array=json_decode($json);) . Other tips include avoiding deep conversions, specifying custom options, and using third-party libraries.

How to use PHP functions to process JSON data? How to use PHP functions to process JSON data? May 04, 2024 pm 03:21 PM

PHP provides the following functions to process JSON data: Parse JSON data: Use json_decode() to convert a JSON string into a PHP array. Create JSON data: Use json_encode() to convert a PHP array or object into a JSON string. Get specific values ​​of JSON data: Use PHP array functions to access specific values, such as key-value pairs or array elements.

See all articles