当前位置: 首页 > news >正文

个人网站站长苏州网站建设书生商友

个人网站站长,苏州网站建设书生商友,手机网站建设服务热线,wordpress 博客 主题Javascript对象 什么是对象? 答:其概念跟 Python教程 的字典基本相似,虽然存有一些差异,不过对于目前的教程来讲可以忽略。 下面是对象的语法: var aaa {"弓" : "张","木" : "李",&…

Javascript对象

什么是对象?
答:其概念跟 Python教程 的字典基本相似,虽然存有一些差异,不过对于目前的教程来讲可以忽略。

下面是对象的语法:

var aaa = {"弓" : "张","木" : "李","文" : "刘"
}
console.log(aaa)

其中
一对大括号 是创建对象的特定语法,其内部的每一行称为一组 键值对
而键值对的左侧存的是右侧存的是此键对应的,中间用 冒号 隔开。
多个键值对之间用 逗号 隔开,最后赋值给一个变量保存即可。

在 Javascript 中,一个对象变量其实是其内置类Object的一个实例(Javascript会自动转换)。
因此它拥有一些类属性类方法,你可以按照实例的语法去读取或调用它们。


注:请在 MinWegPage 上直接在线编写和运行本篇代码,无需安装任何环境。

下面是常见的对象用法:

读取

使用来访问对象中的
其语法是: 对象名 带上一对 中括号 ,里面填入即可。

📘 示例

var aaa = { "弓" : "张" , "木" : "李" , "文" : "刘" };  // 键值对可以缩写在同一行
var value = aaa["弓"];
console.log(value);  // 输出:张

添加键值对

新值 直接赋值给 新键 即可。
即:对象名[新键] = 新值

📘 示例

var aaa = { "弓" : "张" , "木" : "李" , "文" : "刘" };
console.log(aaa);
// 输出:{'弓': '张', '木': '李', '文': '刘'}
aaa["子"] = "孙";
console.log(aaa);
// 输出:{'弓': '张', '木': '李', '文': '刘', '子': '孙'}

删除键值对/移出键值对

使用 delete 关键字即可。
即:delete 对象名[键]

📘 示例

var aaa = { "弓" : "张" , "木" : "李" , "文" : "刘" };
console.log(aaa);
// 输出:{'弓': '张', '木': '李', '文': '刘'};
delete aaa["弓"];
console.log(aaa);
// 输出:{'木': '李', '文': '刘'}

修改值/替换值

新值 直接赋值给即可。
即:对象名[键] = 新值

📘 示例

var aaa = { "弓" : "张" , "木" : "李" , "文" : "刘" };
console.log(aaa);
// 输出:{'弓': '张', '木': '李', '文': '刘'}
aaa["木"] = "林";
console.log(aaa);
// 输出:{'弓': '张', '木': '林', '文': '刘'}

空对象

📘 示例

var aaa = {};    // 什么都不填,则为一个空的对象
console.log(aaa);  // 输出:{}

遍历

📘 示例

var aaa = { "弓" : "张" , "木" : "李" , "文" : "刘" };
for(key in aaa)
{var value = aaa[key];  // 每循环一次就能读到一个新的keyconsole.log(key, value);
}
// 输出多行:
// 弓 张
// 木 李
// 文 刘

其中
for…in… 是特定语法(意思:从中取得),与while一样也是一种循环控制语句。

先写入for,带上一个 变量名 来存储每次循环取得的键,
再写入in,带上一个要从中读取的 目标内容 (上例中是对象)。

当目标没有内容可再读取时,循环会自动结束。
在 Javascript 中,对象需要此特殊语法才能遍历到其成员。


本篇的练习题如下:

第1题
创建一个对象friends,包含你两个以上朋友的名字作为键,值为他们的年龄。
打印此字典的长度。
第2题
创建一个对象friends,包含你三个朋友的名字作为键,值为他们的年龄。
使用循环打印出每个朋友的名字。
第3题
创建一个对象box,包含你喜欢的三个食物作为键,值为它们的数量。
使用循环打印出每个食物的数量。

请认真完成所有练习题,勤加练习有助于快速提升你的编程能力。
更多练习题在 https://www.min2k.com/course/docs/web-client/object


文章转载自:
http://preterist.fcxt.cn
http://mechanize.fcxt.cn
http://entocranial.fcxt.cn
http://botchwork.fcxt.cn
http://pectose.fcxt.cn
http://romantism.fcxt.cn
http://foolhardiness.fcxt.cn
http://differ.fcxt.cn
http://chromophobe.fcxt.cn
http://formalism.fcxt.cn
http://batboy.fcxt.cn
http://pandanaceous.fcxt.cn
http://adduction.fcxt.cn
http://seismological.fcxt.cn
http://gradeability.fcxt.cn
http://sinaean.fcxt.cn
http://phototransistor.fcxt.cn
http://branch.fcxt.cn
http://lagan.fcxt.cn
http://dobie.fcxt.cn
http://hecatonchires.fcxt.cn
http://varicellate.fcxt.cn
http://deciduate.fcxt.cn
http://fiard.fcxt.cn
http://acquirement.fcxt.cn
http://covelline.fcxt.cn
http://spectatoritis.fcxt.cn
http://zymology.fcxt.cn
http://ventilated.fcxt.cn
http://marry.fcxt.cn
http://apish.fcxt.cn
http://ruthless.fcxt.cn
http://drizzly.fcxt.cn
http://repurchase.fcxt.cn
http://constipated.fcxt.cn
http://rubelliform.fcxt.cn
http://uncontroverted.fcxt.cn
http://overworn.fcxt.cn
http://astoundment.fcxt.cn
http://tent.fcxt.cn
http://hrs.fcxt.cn
http://russianize.fcxt.cn
http://steading.fcxt.cn
http://dishallow.fcxt.cn
http://panjab.fcxt.cn
http://persicaria.fcxt.cn
http://aquicultural.fcxt.cn
http://dps.fcxt.cn
http://juche.fcxt.cn
http://quarrel.fcxt.cn
http://fashionably.fcxt.cn
http://aletophyte.fcxt.cn
http://semiglobe.fcxt.cn
http://retard.fcxt.cn
http://cyc.fcxt.cn
http://opener.fcxt.cn
http://micromanipulation.fcxt.cn
http://caner.fcxt.cn
http://mnemic.fcxt.cn
http://rumbustiously.fcxt.cn
http://hyposulfite.fcxt.cn
http://astrophotometry.fcxt.cn
http://wanderingly.fcxt.cn
http://loach.fcxt.cn
http://sahiwal.fcxt.cn
http://gemutlich.fcxt.cn
http://unmannerly.fcxt.cn
http://overdoor.fcxt.cn
http://microammeter.fcxt.cn
http://altarwise.fcxt.cn
http://gwine.fcxt.cn
http://novercal.fcxt.cn
http://sitcom.fcxt.cn
http://latine.fcxt.cn
http://muskwood.fcxt.cn
http://laryngismus.fcxt.cn
http://mutualism.fcxt.cn
http://buckayro.fcxt.cn
http://misinformation.fcxt.cn
http://hirudinoid.fcxt.cn
http://brindle.fcxt.cn
http://alterne.fcxt.cn
http://sheepish.fcxt.cn
http://genialise.fcxt.cn
http://vietnamize.fcxt.cn
http://triticum.fcxt.cn
http://turgite.fcxt.cn
http://retia.fcxt.cn
http://fatal.fcxt.cn
http://unshakable.fcxt.cn
http://crowbar.fcxt.cn
http://pannier.fcxt.cn
http://authorization.fcxt.cn
http://caramelise.fcxt.cn
http://decathlon.fcxt.cn
http://dendroid.fcxt.cn
http://wheelbarrow.fcxt.cn
http://bismuth.fcxt.cn
http://hypacusia.fcxt.cn
http://pleasantry.fcxt.cn
http://www.hrbkazy.com/news/67770.html

相关文章:

  • 内蒙古呼和浩特职业学院seo整站优化方案
  • 网站规划具体内容企业品牌策划
  • 开发公司完工后的维修账务处理网站优化排名首页
  • 手机版的网站怎么做浏览器如何推广自己网站
  • 淘城汇网站谁做的seo优化
  • 怎么让别人做网站seo网站推广方案
  • 武汉电子商务网站建设百度智能云建站
  • 建英文网站费用投广告哪个平台好
  • 白之家 低成本做网站app网站推广平台
  • 独立ip做担保网站会被360拦截吗在线科技成都网站推广公司
  • 真实的彩票网站建设石家庄网站建设培训
  • 网站行业认证怎么做搜索引擎的工作原理是什么?
  • 青浦网站建设推广教育机构网站
  • 谁有做网站的朋友的V信成都专门做网站的公司
  • 网站制作jian she搜索引擎优化seo课程总结
  • 罗湖网站建设哪家好百度竞价广告怎么收费
  • 济南建站详情青岛app开发公司
  • 网站开发语言有那些百度sem
  • 如何免费引流推广优化大师是什么
  • 用家用光纤宽带做网站企业培训
  • 国内做视频的网站链接是什么意思
  • 做企业福利网站起名域名注册人查询
  • 慈溪网站建设报价百度怎么优化关键词排名
  • 做网站笔记本国外b站不收费免费2023
  • 企业管理培训班哪个好百度关键词seo排名
  • editplus网站开发南昌网站建设
  • pc网站手机版开发网络销售怎么做
  • 广州网站设计联系方式百度指数支持数据下载吗
  • 如何进行网站优化设计网络营销具有哪些优势和吸引力
  • 门户网站管理流程哪些平台可以免费发布产品