当前位置:网站首页>Three ways to create objects in JS
Three ways to create objects in JS
2022-04-23 09:27:00 【Small white egg tart】
Catalog
utilize new Object() Create objects
Using constructors to create objects
Traversing objects and methods
The difference between variable attributes
The difference between function and method
An object is an unordered collection of related properties and methods
Three ways to create objects
Literal creation
var stu = {
name: ' Xiao Ming ',
age: 12,
study: function() {
console.log(' I'm learning ');
}
}
1. Objects are stored in key value pairs , key Property name : value Property value .
2. Multiple attributes or methods are separated by commas .
3. There are two ways to use the properties in an object
object . Property name And the object .[' Property name '], The second method must be quoted .
console.log(stu.name);
console.log(stu['age']);
4. Method of calling object
object . Method ()
stu.study();
utilize new Object() Create objects
Use the equal sign assignment method to add the properties and methods of the object .
Each property and method is separated by a semicolon .
var obj = new Object();
obj.name = ' Xiaohong ';
obj.age = 13;
obj.study = function() {
console.log(" Xiao Hong is studying ");
}
obj.study();
Using constructors to create objects
The first two methods can only create objects once at a time , It takes a lot of code , trouble . We can use the method of function , Load the same code , Encapsulate these objects , Is to abstract out the same attributes and methods in the object , Encapsulate it in a function .
1. The constructor name is capitalized , amount to java Class name in .
2. The constructor must write this, Otherwise, multiple object points will be confused , I can't tell who is who .
3. To create an object, add new Constructor name ()
function Star(name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
this.print = function(write) {
console.log(write);
}
}
var obj1 = new Star(' Xiaohong ', 24, ' Woman ', ' Write ');
var obj2 = new Star(' Xiao Ming ',12,' male ');
obj1.print(' Write ');
console.log(obj1);
Traversing objects and methods
for( Variable in object ){}
k It's a variable. , If traversal k What you get is the attribute name ,stu[k] What you get is the attribute value . It doesn't need to be in there 1 Put quotes , because k It's a variable .
var stu = {
name: ' Xiao Ming ',
age: 12,
study: function() {
console.log(' I'm learning ');
}
}
for(k in stu){
console.log(k);
}
for (k in stu) {
console.log(stu[k]);
}
The difference between variable attributes
Variables are declared and assigned separately , When using, write the variable name directly , Exist alone
The attribute is inside the object , No declaration required , But when you use it, you must use the object . attribute
The difference between function and method
What's outside is called a function , The method in the object is called , What they have in common is to realize some function and do something
Functions are declared separately , Direct function name when calling () Exist alone
Method is inside the object , When called, the object . Method ().
版权声明
本文为[Small white egg tart]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230624114235.html
边栏推荐
- DMP engine work summary (2021, lightsaber)
- DJ music management software pioneer DJ rekordbox
- Canary publishing using ingress
- node安装
- Go language self-study series | golang structure pointer
- Using JS to realize a thousandth bit
- Kettle experiment (III)
- Failed to download esp32 program, prompting timeout
- Pre parsing of JS
- How to render web pages
猜你喜欢

《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路

Chapter VIII project stakeholder management of information system project manager summary

Mini - exercice MySQL (seulement pour les débutants, pas pour les non - débutants)
![Cloud computing competition -- basic part of 2020 competition [task 3]](/img/a2/36ff5eafd18534207e6ab01422ea59.png)
Cloud computing competition -- basic part of 2020 competition [task 3]

653. Sum of two IV - input BST

Principle of synchronized implementation

501. 二叉搜索树中的众数
![Buuctf [actf2020 freshman competition] include1](/img/47/b8f46037f7e9476b8e01e8d6a7857a.png)
Buuctf [actf2020 freshman competition] include1

Kettle实验 (三)

MySQL of database -- basic common query commands
随机推荐
小女孩行走
Data visualization: use Excel to make radar chart
Kettle experiment
Creation of raid0 and RAID5 and Simulation of how RAID5 works
653. 两数之和 IV - 输入 BST
Secrets in buffctf file 1
Go language self-study series | golang structure pointer
What is augmented reality technology? Where can it be used?
1 + X cloud computing intermediate -- script construction, read-write separation
成功的DevOps Leader 应该清楚的3个挑战
三、6【Verilog HDL】基础知识之门级建模
Emuelec compilation summary
To remember the composition ~ the pre order traversal of binary tree
Flink 流批一体在小米的实践
About CIN, scanf and getline, getchar, CIN Mixed use of getline
DMP engine work summary (2021, lightsaber)
Little girl walking
小程序报错 :should have url attribute when using navigateTo, redirectTo or switchTab
Summary of common concepts and problems of linear algebra in postgraduate entrance examination
kettle庖丁解牛第14篇之JSON输入