04-JS中的面向对象ES5

news/2024/10/6 1:18:46/文章来源:https://www.cnblogs.com/yufc/p/18288548

01 JS对象中key的类型

02 创建对象的方法

03 对象的常见操作

3.1 访问对象的属性

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script>var person = {name: "kobe",age: 30,height: 1.98}console.log(person.age)console.log(person.height)</script>
</body>
</html>

3.2 修改对象的属性

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script>var person = {name: "kobe",age: 30,height: 1.98}person.age = 25console.log(person.age)</script>
</body>
</html>

3.3 给对象中添加属性

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script>var person = {name: "kobe",age: 30,}person.height = 1.98console.log(person.height)</script>
</body>
</html>

该属性存在就修改,不存在就添加上去

3.4 删除对象中的属性

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script>var person = {name: "kobe",age: 30,}delete person.ageconsole.log(person.age)</script>
</body>
</html>

04 对象的方括号使用

主要是因为key是一个字符串的原因

05 对象的遍历

5.1 for循环示例

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script>var info = {name: "kobe",age: 30,height: 1.98}var infokeys = Object.keys(info)for (var i = 0; i < infokeys.length; i++) {console.log(infokeys[i])}</script>
</body>
</html>

5.2 for ... in ...

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script>var info = {name: "kobe",age: 30,height: 1.98}for (var key in info) {console.log(info[key])}</script>
</body>
</html>

06 内存分配初步理解图

07 js中一些现象的理解

7.1 现象1: 2个对象的比较

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script>var obj1 = {}var obj2 = {}console.log(obj1 === obj2)</script>
</body>
</html>

因为在js中只要是对象就一定会在堆内存中创建一个新的对象,就意味着它们在堆内存中会有2个不同的地址,所以它们肯定是不等的

7.2 现象2: 引用赋值

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script>var info = {name: "kobe",friend: {name: "james"}}var friend = info.friendfriend.name = "john"console.log(info.friend.name)</script>
</body>
</html>

08 创建一系列对象的方式

8.1 第1种方式: 字面量的方式

这种方式虽然可以创建,但是可以发现它们是非常类似,只有属性的值不一样,这样写就会写大量的重复代码

8.2 第2种方式: 函数的方式

8.3 第3种方式: 通过js提供的方式[ES5]

new一个对象时,js帮我们做的事情如下

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/739796.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

程序员的AI工作流

AI 工具在日常工作中的应用逐渐成为程序员必备利器。本文介绍了作者常用的一些 AI 工具及使用方式,涵盖需求文档分析、技术文档编写、编程、PR/CR 和技术调研等工作内容,为提升工作效率提供了有力支持。作为一名程序员, 我现在已经深刻的体会到了AI带来的巨大的工作提升 本文…

An Attentive Inductive Bias for Sequential Recommendation beyond the Self-Attention

目录概符号说明BSARec (Beyond Self-Attention for Sequential Recommendation)代码Shin Y., Choi J., Wi H. and Park N. An attentive inductive bias for sequential recommendation beyond the self-attention. AAAI, 2024.概 本文在 attention block 中引入高低频滤波. 符…

[Leetcode]经典算法

检测环 快慢指针法是一种用于检测链表中是否存在环的有效方法,同时也可以找到环的起点。该方法的原理基于两个指针在链表上同时移动,其中一个移动得更快,而另一个移动得更慢。检测环的存在:使用两个指针,一个称为快指针(fast),一个称为慢指针(slow)。 在每一步中,快…

关于import multiprocessing引用出错

关于import multiprocessing引用出错 0. 原因 当前文件名与python包体中关键词出现同名,导致循环引用 1. 排查过程 问题代码 import timefrom multiprocessing import Process, Queue # 这里提示错误def producer(queue):queue.put("a")time.sleep(2)def consumer(q…

进程信号

进程信号的产生,本质,进程信号的操作,进程信号的底层实现,以及阻塞信号,屏蔽信号1. 信号的产生 1.1 信号概念在生活中有很多的信号在我们身边围绕,例如红绿灯,发令枪,上课铃等等 在接受到信号,我们可以做出三种动作 1.立马去做对应信号的事情 2.等一会再做,有自己的…

24-暑假软件工程日报(7_7)

工作时间:7月7日 14:00-17:00 工作内容: 基本完成第二阶段大程序构建 代码:#include <cstring> #include <iostream> #include <list> #include <math.h> #include <queue> #include <stack> #include <stdio.h> #include <st…

[CISCN2019 华北赛区 Day2 Web1]Hack World

进入题目 输入数字1数字20对select 空格 union or 等等测试发现没有过滤select 空格也被过滤 注意不能单独测试用亦或运算 1^0为真 尝试0^if((ascii(substr((select(flag)from(flag)),1,1))=100),0,1) 回显正常根据回显判断正误 编写脚本爆破,由于该网站请求太快会报429,请求…

CubeMx的部分配置显示不出来

现象描述:CubeMx的部分配置显示不出来 处理方法:(1)继续进行其他配置,给工程起名字,并生成代码;(2)关闭CubeMx后再次打开

[CISCN2019 华东南赛区]Double Secret

进入题目由于请求不能过快,目录扫描工具失效可写脚本, 根据题目两个secret,猜测有serect目录 访问猜测还有一个secret参数随意输入发现源码泄露 注意到有flask,考虑python模板注入 注意到发现rc4加密 找师傅的加密脚本 import base64 from urllib.parse import quote def rc4…

[CISCN2019 华东南赛区]Web11

进入题目注意到xff 在url处随意输入目录xff随之变化 注意下放smarty是php模板 猜测xff为模板注入点 如下用if标签看到回显得到flag flag{6efda977-94fb-4d30-8668-fe28458ec2bf}

game1

进入题目发现是一个游戏发现有一个score.php的发包 发现有分数等 对比不同分数的包发现sign值都有ZM后疑似为base64于是将分数改为较高的分,ZM+base64 尝试要补一个=得到flag

[CISCN 2019 初赛]Love Math

进入题目,直接源码 代码审计、看师傅wp有黑名单字符过滤,有白名单函数过滤 于是用编码绕过,利用eval函数执行命令,system(cat /flag.txt) base_convert(a,b,c) //将数值a按b进制转换为c进制 dechex //将10进制转成16进制 hex2bin() //16进制转成字符串 base_convert(37907…