In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.