Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of object definition in javascript

Shulou Source: shulou.com Published: 2022-06-03 05:07:41 11月04日 Update

This article introduces the relevant knowledge of "what is the method of object definition in javascript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Definition method: 1, with "var object variable name = new Object ();" statement; 2, with "var object variable name = {.}" statement; 3, with "function constructor ([parameter list]) {.} var object name = new constructor ([parameter list]);" statement.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

The method of defining objects in javascript

1. Direct definition

Create syntax:

Var object variable name = new Object (); object variable name. Attribute 1 = attribute value 1; … ; object variable name. Attribute N = attribute value N; object variable name. Method 1 = function ([parameter list]) {method body}... ; object variable name. Method N = function ([parameter list]) {method body}

Code example:

Var student = new Object (); student.name= "Lucy"; student.eat=function () {console.log (this.name+ "eating");} student.eat ()

2. Initialization definition:

Create syntax:

Var object variable name = {attribute 1: attribute value 1,..., attribute N: attribute value N, method 1:function ([parameter list]) {method body},..., method N:function ([parameter list]) {method body}}

Note:

1. Attribute is defined with: (English colon)

two。 The object's properties and methods are separated by (English comma), and the last one is not added.

Code example:

Var student = {name: "Tim", / / Note attributes are defined with: age:12, eatting:function () {console.log (this.name+ "eating");}, running:function () {console.log (this.name+ "running");}; student.eatting (); student.running ()

3. Constructor formula

Create syntax:

Function constructor ([parameter list]) {this. Attribute 1 = attribute value;...; this. Attribute N = attribute value; this. Method 1=function ([parameter list]) {method body};...; this. Method N=function ([parameter list]) {method body};} var object name = new constructor ([parameter list])

Code example:

Function Student (name) {this.name=name; this.eatting=function () {console.log (this.name+ "eating");};} var stu = new Student ("Lily"); stu.eatting (); "what is the method of object definition in javascript". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Methods properties objects parameters variables functions being things examples statements syntax code content more knowledge English practical learned and then between Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Linux Microsoft Redmi macOS