未加星标

The Chronicles of JavaScript Objects

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏
The Chronicles of javascript Objects

Arfat Salman

Objects are the foundation of JavaScript and permeate its every aspect. Almost everything in JavaScript is an object. In fact, only six things are not objects. They are ― null , undefined , strings , numbers , boolean , and symbols . These are called primitive values or primitive types.

Anything that is not a primitive value is an Object . That includes arrays , functions , constructors, and objects themselves. Yes! Functions and arrays are objects too as we shall see later in the article.


The Chronicles of JavaScript Objects
In JavaScript, it is :smiley: ! (via Giphy ) What areobjects?

Just like other primitive values, objects are values too and they serve the same purpose: Representing information or data . When we create a variable and assign a value to it, we are holding information in it for later retrieval and processing. For example ―

const <strong>firstName</strong> = 'Arfat Salman';

In this case, firstName holds and represents the first name of a person. In a sense, strings are called primitive values because they can represent only one type of information. In this case, a collection of characters or a string.

What happens when we want to represent a person as a whole ? That is, I want to hold on to first name (String), last name (String) , date of birth (Date) , and her friends (Collection of Strings) . If I create four variables for each datum, representing three persons would require 4 * 3 = 12 variable names. That’s not good!

In some languages, this question would segue into a discussion about user-defined data types or custom data types . Constructs such as classes, structures , unions types etc. can be used to achieve that. However, JavaScript does not have classes (even though it has a class keyword), or structures. It only has objects for creating custom data types and associating behavior.

In JavaScript, an object can be thought of as a distinct entity that can have properties associated with it. These properties define the object’s characteristics and behavior. In our person example, an object can have 4 properties describing it ― first name, last name, DOB, and friends.


The Chronicles of JavaScript Objects
Here’s an example representation of “ person” object.

Here’s how ECMA specification defines an object ―

An Object is logically a collection of properties.

The properties are also known as key-value pairs . You may come across similar concepts in the form of Associative Arrays , maps , and dictionaries in other languages. JavaScript objects embody those characteristics, but they also have a lot more to them as we shall see.


The Chronicles of JavaScript Objects
Objects and Properties

We are going to examine objects by the kind of operation we can perform on them. The operations are ―

Creation ― Creating a new object. Addition ― Adding properties to an object. Reading/Retrieving ― Reading the value associated with a key. Existence ― Whether a property exists in an object. Updation ― Updating the values of existing properties. Deletion ― Deleting existing properties. Iteration (Enumeration) ― Looping over an object’s keys and values. Comparison ― Comparing two different objects. Copying ― Making a copy of an existing object Creation

Properties of an object are just like variables. Let’s see how can we create objects and properties ―

Creating an object using “literal” notation.

A literal is a notation for representing a fixed value in source code. For example, 5 or "foo" . They literally represent the value. An object is represented as ―

const obj = <strong>{ }</strong>; // <strong>Creating an object</strong> using literal notation

The curly braces { } create a new object.

You can also specify properties to be inserted at the time of creation.

const obj = {
firstName: 'Alex',
'lastName': 'Martin', // Using string quotes
dateOfBirth: '18th October',
friends: [ 'Bob', 'Christine' ]
};

The property names are treated as strings. That is, firstName and 'firstName' are same. An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string.

Note that the values can be any value type. That is, both primitive types and an object can be used as values. const obj = {
nestedObj: {
name: 'Alex'
}
}

The nestedObj is a key that holds another object. The nesting can be arbitrary and cyclic too!

Creating objects using Object constructor const obj = <strong>new Object();</strong>

This effect of this code and the literal notation is the same. However, it is advised not to use this pattern. You can read more about it here .

Computed Properties

You can also use an expression that will be evaluated to a string using the new computed property syntax. As an example ―

const propertyName = 'firstName';
const obj = {
[propertyName.toUpperCase()]: 'Alex',
} // { FIRSTNAME: 'Alex' } Note the variable propertyName . In obj , we wrap the expression propertyName.toUpperCase() in [ ] to make it a computed property . As expected, in the resulting output, the string firstName is turned into uppercase. Function as keyvalues In an object, when we ha

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:The Chronicles of JavaScript Objects
本站链接:https://www.codesec.net/view/627943.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(43)