当前位置:网站首页>Objects in JS
Objects in JS
2022-07-25 11:47:00 【c_ perfectworld】
object
One Concrete thing , A group of A disorderly relevant attribute and Method Set , All things are objects
Object by attribute and Method form
- attribute : Things features , Use in objects attribute Express ( Common nouns )
- Method : Things Behavior , Use in objects Method Express ( Common verbs )
Three ways to create objects
- utilize Literal Create objects
- utilize new object Create objects
- utilize Constructors Create objects
Create an object using literals
Object literal : Curly braces {} It contains the expression of this specific thing ( object ) Properties and methods of
- The attributes or methods inside take Key value pair In the form of key Property name : value Property value
- Multiple attributes or methods are separated by commas
- The method colon is followed by an anonymous function
utilize new Object Create objects
var obj = new Object();- Use the equal sign = The method of assignment Add the properties and methods of the object
- Each property and method ends with a semicolon
obj.uname = ' Zhang Sanfeng ';
obj.sex = ' male ';
obj.sayHi = function () {
console.log('Hi');
}Using constructors to create objects
reason : The first two methods of creating objects can only Create a object , Many properties and methods are the same , So using the constructor method , Repeat the same code , Let's put this function It's called a constructor
Because this function is not encapsulated in ordinary code , But the object , Constructor is to abstract the same attributes and methods and encapsulate them into functions
The syntax format of the constructor
function Constructor name () {
this. attribute = value ;
this. Method = function () {
}
}
// Using functions
new Constructor name ();1、 The constructor name should be capitalized
2、 Constructors don't need return You can return the result
3、 Calling the constructor must use new
4、 as long as new Star() Calling the function creates an object
5、 Properties and methods must be preceded by this
Use object
(1) Properties of the calling object , take Object name . Property name In the form of . Understood as
(2) Call the second property Object name [' Property name ']
(3) Method of calling object , Object name . Method name () Don't forget the parentheses
Variable 、 attribute 、 function 、 Differences in methods
The same thing :
The similarities between variables and attributes are used to Store the data Of
Functions and methods are To achieve a function
Difference :
Variable Make a separate statement And the assignment , When using direct Write variable name ( Exist alone )
The attribute is Inside the object , yes No declaration required Of , When using, it must be object . attribute Can be used
The function is Make a separate statement , And call the function name () Exist alone Of
Method , stay Inside the object , Call time object . Method ()
The difference between constructors and objects
An object refers specifically to a Concrete things
Constructor refers to a large class of
The process of creating objects using constructors is also called Object instantiation
new keyword
- new Constructors can be in memory Create an empty object
- this will Point to The object just created
- Execute the code in the constructor , Give constructors add to Properties and methods
- return This object ( So constructors don't need return)
Traversing objects
for...in You can modify the attributes of arrays or objects loop operation
for ( Variable in object ) {
console.log(k) // The property name of the object
console.log( object [k]) // Property value of object
}Use for in The variables inside Like to use k perhaps key
Object summary
- Objects can make the structure clearer
- Objects are complex data types object
- The essence : An object is an unordered collection of related properties and methods
- Constructors generally refer to a large class
- An object instance refers specifically to a thing
边栏推荐
- 阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程
- Information management system for typical works of urban sculpture (picture sharing system SSM)
- Database integrity -- six constraints learning
- SQL injection less17 (error injection + subquery)
- 工作面试总遇秒杀?看了京东T8大咖私藏的秒杀系统笔记,已献出膝盖
- 油猴脚本链接
- Oracle parsing XML with the same name
- Flinksql client connection Kafka select * from table has no data error, how to solve it?
- JVM性能调优方法
- Menu bar + status bar + toolbar ==pyqt5
猜你喜欢

教你如何通过MCU配置S2E为TCP Server的工作模式

大话DevOps监控,团队如何选择监控工具?

Similarity matrix, diagonalization condition

Teach you how to configure S2E as the working mode of TCP client through MCU

WIZnet W5500系列培训活动之“MQTT协议讲解和实践(接入OneNET)”

Filter过滤器解决request请求参数乱码的原理解析

任何时间,任何地点,超级侦探,认真办案!

【mysql学习09】
Definition of information entropy

相似矩阵,可对角化条件
随机推荐
JS常用内置对象 数据类型的分类 传参 堆栈
W5500多节点连接
Wiznet embedded Ethernet technology training open class (free!!!)
如何解决“W5500芯片在TCP_Client模式下,断电重启之后无法立即连接到服务器”的问题
[electronic device notes 5] diode parameters and selection
SQL injection LESS18 (header injection + error injection)
JS中的对象
ESP8266 使用 DRV8833驱动板驱动N20电机
Emmet syntax quick query syntax basic syntax part
Shell - Chapter 6 exercise
小微企业智能名片管理小程序
常见WEB攻击与防御
教你如何通过MCU将S2E配置为UDP的工作模式
Shell Chapter 5 homework
Detailed explanation of zero basis from macro to micro Bert
教你如何通过MCU配置S2E为TCP Server的工作模式
Linked list related (design linked list and ring linked list)
SQL language (I)
Oracle parsing XML with the same name
贪心问题01_活动安排代码分析