当前位置:网站首页>JS中的对象
JS中的对象
2022-07-25 10:54:00 【c_perfectworld】
对象
一个具体的事物,一组无序的相关属性和方法的集合,所有的事物都是对象
对象由属性和方法组成
- 属性:事物的特征,在对象中用属性表示(常用名词)
- 方法:事物的行为,在对象中用方法表示(常用动词)
创建对象的三种方式
- 利用字面量创建对象
- 利用 new object 创建对象
- 利用构造函数创建对象
利用字面量创建对象
对象字面量:花括号 {} 里面包含了表达这个具体事物 (对象)的属性和方法
- 里面的属性或者方法采取键值对的形式 键 属性名 : 值 属性值
- 多个属性或者方法中间用逗号隔开
- 方法冒号后面跟的是一个匿名函数
利用new Object 创建对象
var obj = new Object();- 利用等号 = 赋值的方法 添加对象的属性和方法
- 每个属性和方法之间用分号结束
obj.uname = '张三丰';
obj.sex = '男';
obj.sayHi = function () {
console.log('Hi');
}利用构造函数创建对象
理由:前面两种创建对象方式一次只能创建一个对象,里面很多属性和方法时相同的,所以利用构造函数的方法,重复这些相同的代码,我们就把这个函数称为构造函数
因为这个函数里面封装的不是普通代码,而是对象,构造函数就是把相同的属性和方法抽象出来封装到函数里面
构造函数的语法格式
function 构造函数名 () {
this.属性 = 值;
this.方法 = function () {
}
}
//使用函数
new 构造函数名();1、构造函数名首字母要大写
2、构造函数不需要return就可以返回结果
3、调用构造函数必须使用new
4、只要new Star()调用函数就创建了一个对象
5、属性和方法前面必须添加this
使用对象
(1)调用对象的属性,采取对象名.属性名的形式 .理解为的
(2)调用属性第二种 对象名 ['属性名']
(3)调用对象的方法,对象名.方法名() 千万别忘记小括号
变量、属性、函数、方法的区别
相同点:
变量和属性相同点都是用来存储数据的
函数和方法都是实现某种功能
不同点:
变量单独声明并赋值,使用时直接写变量名(单独存在)
属性是在对象里面,是不需要声明的,使用的时候必须是对象 . 属性才可以使用
函数是单独声明,并且调用函数名()单独存在的
方法,在对象里面,调用时时对象 . 方法()
构造函数和对象的区别
对象是特指某一个具体的事物
构造函数是泛指的某一大类
我们利用构造函数创建对象的过程也叫对象的实例化
new关键字
- new构造函数可以在内存中创建一个空的对象
- this就会指向刚才创建的对象
- 执行构造函数里面的代码,给构造函数添加属性和方法
- 返回这个对象(所以构造函数不需要return)
遍历对象
for...in可以对数组或者对象的属性进行循环操作
for (变量 in 对象) {
console.log(k) //对象的属性名
console.log(对象[k]) //对象的属性值
}使用for in 里面的变量 喜欢用 k 或者 key
对象小结
- 对象可以让结构更清晰
- 对象是复杂数据类型object
- 本质:对象是一组无序的相关属性和方法的集合
- 构造函数泛指某一大类
- 对象实例特指一个事物
边栏推荐
- SQL注入 Less18(头部注入+报错注入)
- [leetcode brush questions]
- flinksql client 连接kafka select * from table没有数据报错,如何解决?
- leetcode 剑指 Offer 27. 二叉树的镜像
- Shell - Chapter 8 exercise
- 动态规划问题05_导弹拦截
- RedisUtil
- Database design - Simplified dictionary table [easy to understand]
- The most complete detailed tutorial on importing ad into lichuanyuan device packaging Library in history (always white and always cool)
- W5500上传温湿度到oneNET平台
猜你喜欢

The most complete detailed tutorial on importing ad into lichuanyuan device packaging Library in history (always white and always cool)

Job interviews are always a second kill? After reading the seckill system notes secretly stored by JD T8, I have given my knees

Common web attacks and defense

W5500在处于TCP_Server模式下,在交换机/路由器网络中无法ping通也无法通讯。

varest蓝图设置json

MySQL | GROUP_ The concat function concatenates the values of a column with commas
Details of the list of state products that Apple announced to be eligible for the sales tax holiday in the United States

Small and micro enterprise smart business card management applet

What kind of product power does Hongguang miniev, the top seller of new energy, have?

谣言检测文献阅读十一—Preventing rumor spread with deep learning
随机推荐
基于cornerstone.js的dicom医学影像查看浏览功能
Teach you how to configure S2E to UDP working mode through MCU
Brief description of model deployment
绘图==PYQT5
"Mqtt protocol explanation and Practice (access to onenet)" of wiznet w5500 series training activities
SQL注入 Less18(头部注入+报错注入)
leetcode 剑指 Offer 27. 二叉树的镜像
使用Three.js实现炫酷的赛博朋克风格3D数字地球大屏
Hardware peripherals =maixpy3
cookie and session
Common linear modulation methods based on MATLAB
世界上最高效的笔记方法(改变你那老版的记笔记方法吧)
【leetcode刷题】
Compressed list ziplist of redis
Leetcode sword finger offer 28. symmetric binary tree
基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
[tree] 100. Same tree
Information management system for typical works of urban sculpture (picture sharing system SSM)
The principle analysis of filter to solve the request parameter garbled code
Learn NLP with Transformer (Chapter 1)