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 a new object for javascript

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

Share

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

This article mainly introduces javascript how to create a new object, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Javascript create a new object method: 1, through the literal form of a new object; 2, through the new Object () form of a new object.

This article operating environment: windows7 system, javascript1.8.5 version, Dell G3 computer.

There are two syntaxes for creating an object in JavaScript:

One is in the form of literal quantity, the other is in the form of new Object ().

Create a person object that has properties such as name,age,sex.

1. Literal quantity form (literal syntax)

Var person = {name: "chen", age:12,sex: "male"}

2. New Object () form

Var person = new Object (); person.name= "test"; person.age=12; person.sex= "male"

When declaring an object in JS, people often write var a = {}; few people write this: var a = new Object ()

Why?

{} this is called an object literal, and new Object () uses the constructor function.

The literal amount of an object is declared more conveniently than a constructor function.

Therefore, it is suggested that the declaration of the literal quantity of the object should be given priority in JS.

In JavaScript, using the new keyword means that you have done four things:

Create a new object of type object

Set the interior, accessibility, and [[prototype]] properties of this new object to the

Executes the constructor to use the properties of the newly created object when the this keyword is mentioned

Returns the newly created object (unless no prototype is returned in the constructor).

Thank you for reading this article carefully. I hope the article "how to create a New object in javascript" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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