当前位置:网站首页>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
边栏推荐
- flinksql client 连接kafka select * from table没有数据报错,如何解决?
- 微星主板前面板耳机插孔无声音输出问题【已解决】
- Teach you how to configure S2E to UDP working mode through MCU
- WIZnet嵌入式以太网技术培训公开课(免费!!!)
- Hacker introductory tutorial (very detailed) from zero basic introduction to proficiency, it is enough to read this one.
- JS中的函数
- The most efficient note taking method in the world (change your old version of note taking method)
- Summary of combination problems of Li Kou brush questions (backtracking)
- Txt to CSV file, blank lines appear every other line
- JS作用域以及预解析
猜你喜欢

Information management system for typical works of urban sculpture (picture sharing system SSM)
![[leetcode brush questions]](/img/86/5f33a48f2164452bc1e14581b92d69.png)
[leetcode brush questions]

JS流程控制

活动报名 | 玩转 Kubernetes 容器服务提高班正式开营!

How to judge the performance of static code quality analysis tools? These five factors must be considered

Review in the middle of 2022 | understand the latest progress of pre training model

Fillet big killer, use filter to build fillet and wave effect!

Understanding: idea uses Scala to write wordcount programs and generate jar packages

Reflection reflection

小微企业智能名片管理小程序
随机推荐
【电子器件笔记5】二极管参数和选型
SQL language (V)
教你如何通过MCU配置S2E为TCP Client的工作模式
ESP8266 使用 DRV8833驱动板驱动N20电机
矩阵的特征值和特征向量
W5500在处于TCP_Server模式下,在交换机/路由器网络中无法ping通也无法通讯。
W5500通过上位机控制实现调节LED灯带的亮度
大话DevOps监控,团队如何选择监控工具?
相似矩阵,可对角化条件
SQL injection LESS18 (header injection + error injection)
各种控件==PYQT5
Miidock Brief
【mysql学习08】
绘图==PYQT5
硬件外设=maixpy3
JVM performance tuning methods
The principle analysis of filter to solve the request parameter garbled code
W5500上传温湿度到oneNET平台
DICOM medical image viewing and browsing function based on cornerstone.js
ArcMap cannot start the solution