当前位置:网站首页>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
边栏推荐
- Mini - exercice MySQL (seulement pour les débutants, pas pour les non - débutants)
- ALV树(LL LR RL RR)插入删除
- ALV tree (ll LR RL RR) insert delete
- 数据清洗 ETL 工具Kettle的安装
- MySQL of database -- Fundamentals
- To remember the composition ~ the pre order traversal of binary tree
- Get trustedinstaller permission
- Leetcode-199 - right view of binary tree
- Failed to download esp32 program, prompting timeout
- 搞不懂时间、时间戳、时区,快来看这篇
猜你喜欢
MySQL of database -- overview and installation
Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
112. 路径总和
To remember the composition ~ the pre order traversal of binary tree
Harbor enterprise image management system
STM32 and FreeRTOS stack parsing
Kettle experiment (III)
Principle of synchronized implementation
[reading notes] Chapter 5 conditional statements, circular statements and block statements of Verilog digital system design tutorial (with answers to thinking questions)
108. Convert an ordered array into a binary search tree
随机推荐
1 + X cloud computing intermediate -- script construction, read-write separation
DJ music management software pioneer DJ rekordbox
RSA 加密解密签名验签
LGB, XGB, cat, k-fold cross validation
nn. Explanation of module class
web页面如何渲染
Unfortunately, I broke the leader's confidential documents and spit blood to share the code skills of backup files
Matlab draw five-star red flag
Sql1 [geek challenge 2019]
[Luke V0] verification environment 2 - Verification Environment components
Number of islands
Project upload part
What is monitoring intelligent playback and how to use intelligent playback to query video recording
Yyds dry goods inventory ubuntu18 0.4 install MySQL and solve error 1698: access denied for user ''root' '@' 'localhost' '
Pre parsing of JS
How does kubernetes use harbor to pull private images
Write down the post order traversal of the ~ binary tree
Arbre de dépendance de l'emballage des ressources
Open services in the bottom bar of idea
NPM installation yarn