首页 > 编程语言 >[Javascript] How javascript read the property?

[Javascript] How javascript read the property?

时间:2024-10-08 15:12:45浏览次数:6  
标签:obj read symbol object How const property nope

As we know we can read property value from an object as so:

const obj = {}

obj.xxxx;
obj[xxxx];

 

So what's the difference between those two?

 

obj.x

ECMAScript will do a transform internally as such

[[GET]](obj /*object itself*/, 'x' /*property key*/, obj /*this pointer*/)

 

obj[x]

ECMAScript will do a transform internally as such

[[GET]](
    obj /*object itself*/, 
    x is symbol ? x: String(x) /*check whether x is symbol, if is, then use symbol, otherwise stringify x*/, 
    obj /*this pointer*/
)

 

So as we can see, the only difference between those two is how ECMAScript handle x property key.

obj.x: consider 'x' as string value

obj[x]: consider x can be smybol or not, if yes, will use symbol, otherwise, convert to string value

 

Examples

const obj = {}
obj[0] = 1 // is 0 a symbol? nope, then conver to '0'
obj['0'] = 2 // is '0' a symbol? nope, then keep '0'
console.log(obj) // {'0': 2}

 

const obj = {}

obj[obj] = 1 // is obj a symbol? nope, then String(obj) which is '[object object]'
console.log(obj) // {'[object Object]': 1}

 

 

const obj = {
  toString() {
    return 'abc'
  }
}

obj[obj] = 1 // is obj a symbol? nope, then String(obj) which is 'abc'
console.log(obj) // {toString: [Function: toString], 'abc': 1}

 

标签:obj,read,symbol,object,How,const,property,nope
From: https://www.cnblogs.com/Answer1215/p/18451674

相关文章

  • abc373E How to Win the Election
    有N个候选人和总共K张选票,目前第i个候选人的票数为A[i]。在全部选票统计完成后,如果得票数多于自己的人数小于M,则当选,可以多个人同时当选。对于每个人,输出当选需要再获得的最少票数。1<=M<=N<=2E5,1<=K<=1E12,0<=A[i]<=1E12,sum(A[i])<=K分析:对每个候选人,二分答案,假设需要的票......
  • `std::packaged_task`、`std::thread` 和 `std::async` 的区别与联系
    std::packaged_task、std::thread和std::async的区别与联系std::packaged_task、std::thread和std::async都是C++11中提供的并发工具,用于执行任务并处理多线程操作。虽然它们都有类似的作用(并发执行任务),但在功能和使用方式上有显著区别。下面分别解释它们的特点,并说明它......
  • FileReader 字节输入流
    publicclassFileReaderTest{publicstaticvoidmain(String[]args){Readerfr=null;try{//2.实例化FileReader对象fr=newFileReader("D:\\doc\\诗词.txt");char[]ch=newchar[1024];//创建字符数组......
  • 【国产化】RT-THREAD ENV介绍和应用
    原创zgrxmmlinux源码阅读前言作为一款优秀的国产实时操作系统,RT-THREAD以其轻量级、高可靠性和丰富的生态系统,在众多嵌入式开发项目中崭露头角。今天,基于RT-THREAD探索如何利用其强大的ENV工具链快速搭建和配置STM32F429-ATK-APOLLO开发板的工程项目。通过这篇教程......
  • A_H_README_TO_RECOVER勒索恢复---惜分飞
    联系:手机/微信(+8617813235971)QQ(107644445)标题:A_H_README_TO_RECOVER勒索恢复作者:惜分飞©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]有客户mysql数据库被黑(业务数据库被删除),创建了一个A_H_README_TO_RECOVER库[[email protected]......
  • const和readonly修饰的成员,静态构造函数以及对于变量的访问{get;set}
    第一,const修饰的数据类型定义:按照publicconstinttest=20;的格式进行声明,const修饰的数据类型叫做常量。注意:1访问时只能通过类名加变量名访问。      2必须在声明的时候就赋值。      3常量是不可修改的值。代码如下:usingSystem.Collection......
  • How to use the Ubuntu
    HowtousetheUbuntuIntroduction‍AsfarasIhavenoted,IwillforcemyselftowriteallmyblogsinEnglish.So,ifyouareluckyorunlucky,youhavereadthisblog,andIhopeitcanbehelpfultoyou.ReadinganEnglishdocumentcanbeatrickyt......
  • How to use the Ubuntu
    HowtousetheUbuntuIntroduction‍AsfarasIhavenoted,IwillforcemyselftowriteallmyblogsinEnglish.So,ifyouareluckyorunlucky,youhavereadthisblog,andIhopeitcanbehelpfultoyou.ReadinganEnglishdocumentcanbeatrickyt......
  • Creating, Reading and Writing(pandas学习一)
    开始入门要使用pandas库,通常从以下这行代码开始。importpandasaspd创建数据pandas中有两个核心对象:DataFrame(数据框)和Series(系列)。DataFrameDataFrame是一个表格。它包含一个单独条目的数组,每个条目都有特定的值。每个条目对应一行(或一条记录)和一列。pd.DataFrame......
  • WPF Click Window show XY coordinates in MVVM via InvokeCommandAction of behavior
    1.Install Microsoft.Xaml.Behaviors.Wpf  2.<behavior:Interaction.Triggers><behavior:EventTriggerEventName="MouseDown"><behavior:InvokeCommandActionCommand="{BindingClickCommand}"......