当前位置:网站首页>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
边栏推荐
- Number theory to find the sum of factors of a ^ B (A and B are 1e12 levels)
- JS prototype chain
- Kettle experiment (III)
- Kettle实验 转换案例
- [SQL Server fast track] view and cursor of database
- Notes on xctf questions
- Colorui solves the problem of blocking content in bottom navigation
- tsdf +mvs
- Kettle experiment conversion case
- js 原型链的深入
猜你喜欢
Go language learning notes - language interface | go language from scratch
Simple understanding of arguments in JS
Data visualization: use Excel to make radar chart
搞不懂时间、时间戳、时区,快来看这篇
npm报错 :operation not permitted, mkdir ‘C: \Program Files \node js \node_ cache _ cacache’
Applet error: cannot read property'currenttarget'of undefined
Failed to download esp32 program, prompting timeout
What is monitoring intelligent playback and how to use intelligent playback to query video recording
机器学习(六)——贝叶斯分类器
LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
随机推荐
Notes on xctf questions
web页面如何渲染
Chapter VIII project stakeholder management of information system project manager summary
《信息系统项目管理师总结》第八章 项目干系人管理
Colorui solves the problem of blocking content in bottom navigation
JS case to find the maximum value, reverse the array, bubble sort
Thread scheduling (priority)
Enter "net start MySQL" and "system error 5. Access denied" appears. Detailed explanation of the problem
First principle mind map
SQL used query statements
成功的DevOps Leader 应该清楚的3个挑战
Kettle实验 转换案例
653. 两数之和 IV - 输入 BST
501. Mode in binary search tree
DVWA range practice record
Production practice elk
SAP 101K 411k inventory change
JSON input of Chapter 14 of kettle paoding jieniu
GUI, CLI and UNIX Philosophy
员工试用期转正申请书(泸州老窖)