Cara menggunakan javascript object property number

As we know from the chapter Data types, there are eight data types in JavaScript. Seven of them are called “primitive”, because their values contain only a single thing (be it a string or a number or whatever).

In contrast, objects are used to store keyed collections of various data and more complex entities. In JavaScript, objects penetrate almost every aspect of the language. So we must understand them first before going in-depth anywhere else.

An object can be created with figure brackets user.isAdmin = true;2 with an optional list of properties. A property is a “key: value” pair, where user.isAdmin = true;3 is a string (also called a “property name”), and user.isAdmin = true;4 can be anything.

We can imagine an object as a cabinet with signed files. Every piece of data is stored in its file by the key. It’s easy to find a file by its name or add/remove a file.

An empty object (“empty cabinet”) can be created using one of two syntaxes:

let user = new Object(); // "object constructor" syntax let user = {}; // "object literal" syntax

Usually, the figure brackets user.isAdmin = true;5 are used. That declaration is called an object literal.

We can immediately put some properties into user.isAdmin = true;5 as “key: value” pairs:

let user = { // an object name: "John", // by key "name" store value "John" age: 30 // by key "age" store value 30 };

A property has a key (also known as “name” or “identifier”) before the colon user.isAdmin = true;7 and a value to the right of it.

In the user.isAdmin = true;8 object, there are two properties:

  1. The first property has the name user.isAdmin = true;9 and the value delete user.age;0.
  2. The second one has the name delete user.age;1 and the value delete user.age;2.

The resulting user.isAdmin = true;8 object can be imagined as a cabinet with two signed files labeled “name” and “age”.

We can add, remove and read files from it at any time.

Property values are accessible using the dot notation:

// get property values of the object: alert( user.name ); // John alert( user.age ); // 30

The value can be of any type. Let’s add a boolean one:

user.isAdmin = true;

To remove a property, we can use the delete user.age;4 operator:

delete user.age;

We can also use multiword property names, but then they must be quoted:

let user = { name: "John", age: 30, "likes birds": true // multiword property name must be quoted };

The last property in the list may end with a comma:

let user = { name: "John", age: 30, }

That is called a “trailing” or “hanging” comma. Makes it easier to add/remove/move around properties, because all lines become alike.

For multiword properties, the dot access doesn’t work:

// this would give a syntax error user.likes birds = true

JavaScript doesn’t understand that. It thinks that we address delete user.age;5, and then gives a syntax error when comes across unexpected delete user.age;6.

The dot requires the key to be a valid variable identifier. That implies: contains no spaces, doesn’t start with a digit and doesn’t include special characters (delete user.age;7 and delete user.age;8 are allowed).

There’s an alternative “square bracket notation” that works with any string:

let user = {}; // set user["likes birds"] = true; // get alert(user["likes birds"]); // true // delete delete user["likes birds"];

Now everything is fine. Please note that the string inside the brackets is properly quoted (any type of quotes will do).

Square brackets also provide a way to obtain the property name as the result of any expression – as opposed to a literal string – like from a variable as follows:

let key = "likes birds"; // same as user["likes birds"] = true; user[key] = true;

Here, the variable user.isAdmin = true;3 may be calculated at run-time or depend on the user input. And then we use it to access the property. That gives us a great deal of flexibility.

For instance:

let user = { // an object name: "John", // by key "name" store value "John" age: 30 // by key "age" store value 30 };0

The dot notation cannot be used in a similar way:

let user = { // an object name: "John", // by key "name" store value "John" age: 30 // by key "age" store value 30 };1

We can use square brackets in an object literal, when creating an object. That’s called computed properties.

For instance:

let user = { // an object name: "John", // by key "name" store value "John" age: 30 // by key "age" store value 30 };2

The meaning of a computed property is simple: let user = { name: "John", age: 30, "likes birds": true // multiword property name must be quoted };0 means that the property name should be taken from let user = { name: "John", age: 30, "likes birds": true // multiword property name must be quoted };1.

So, if a visitor enters let user = { name: "John", age: 30, "likes birds": true // multiword property name must be quoted };2, let user = { name: "John", age: 30, "likes birds": true // multiword property name must be quoted };3 will become let user = { name: "John", age: 30, "likes birds": true // multiword property name must be quoted };4.

Essentially, that works the same as:

let user = { // an object name: "John", // by key "name" store value "John" age: 30 // by key "age" store value 30 };3

…But looks nicer.

We can use more complex expressions inside square brackets:

let user = { // an object name: "John", // by key "name" store value "John" age: 30 // by key "age" store value 30 };4

Square brackets are much more powerful than dot notation. They allow any property names and variables. But they are also more cumbersome to write.

So most of the time, when property names are known and simple, the dot is used. And if we need something more complex, then we switch to square brackets.

In real code, we often use existing variables as values for property names.

For instance:

let user = { // an object name: "John", // by key "name" store value "John" age: 30 // by key "age" store value 30 };5

In the example above, properties have the same names as variables. The use-case of making a property from a variable is so common, that there’s a special property value shorthand to make it shorter.

Instead of let user = { name: "John", age: 30, "likes birds": true // multiword property name must be quoted };5 we can just write let user = { name: "John", age: 30, "likes birds": true // multiword property name must be quoted };6, like this:

let user = { // an object name: "John", // by key "name" store value "John" age: 30 // by key "age" store value 30 };6

We can use both normal properties and shorthands in the same object:

let user = { // an object name: "John", // by key "name" store value "John" age: 30 // by key "age" store value 30 };7

As we already know, a variable cannot have a name equal to one of the language-reserved words like “for”, “let”, “return” etc.

But for an object property, there’s no such restriction:

let user = { // an object name: "John", // by key "name" store value "John" age: 30 // by key "age" store value 30 };8

In short, there are no limitations on property names. They can be any strings or symbols (a special type for identifiers, to be covered later).

Other types are automatically converted to strings.

For instance, a number let user = { name: "John", age: 30, "likes birds": true // multiword property name must be quoted };7 becomes a string let user = { name: "John", age: 30, "likes birds": true // multiword property name must be quoted };8 when used as a property key:

let user = { // an object name: "John", // by key "name" store value "John" age: 30 // by key "age" store value 30 };9

There’s a minor gotcha with a special property named let user = { name: "John", age: 30, "likes birds": true // multiword property name must be quoted };9. We can’t set it to a non-object value:

// get property values of the object: alert( user.name ); // John alert( user.age ); // 300

As we see from the code, the assignment to a primitive let user = { name: "John", age: 30, }0 is ignored.

We’ll cover the special nature of let user = { name: "John", age: 30, "likes birds": true // multiword property name must be quoted };9 in subsequent chapters, and suggest the ways to fix such behavior.

A notable feature of objects in JavaScript, compared to many other languages, is that it’s possible to access any property. There will be no error if the property doesn’t exist!

Reading a non-existing property just returns let user = { name: "John", age: 30, }2. So we can easily test whether the property exists:

// get property values of the object: alert( user.name ); // John alert( user.age ); // 301

There’s also a special operator let user = { name: "John", age: 30, }3 for that.

The syntax is:

// get property values of the object: alert( user.name ); // John alert( user.age ); // 302

For instance:

// get property values of the object: alert( user.name ); // John alert( user.age ); // 303

Please note that on the left side of let user = { name: "John", age: 30, }4 there must be a property name. That’s usually a quoted string.

If we omit quotes, that means a variable should contain the actual name to be tested. For instance:

// get property values of the object: alert( user.name ); // John alert( user.age ); // 304

Why does the let user = { name: "John", age: 30, }4 operator exist? Isn’t it enough to compare against let user = { name: "John", age: 30, }2?

Well, most of the time the comparison with let user = { name: "John", age: 30, }2 works fine. But there’s a special case when it fails, but let user = { name: "John", age: 30, }3 works correctly.

It’s when an object property exists, but stores let user = { name: "John", age: 30, }2:

// get property values of the object: alert( user.name ); // John alert( user.age ); // 305

In the code above, the property // this would give a syntax error user.likes birds = true0 technically exists. So the let user = { name: "John", age: 30, }4 operator works right.

Situations like this happen very rarely, because let user = { name: "John", age: 30, }2 should not be explicitly assigned. We mostly use // this would give a syntax error user.likes birds = true3 for “unknown” or “empty” values. So the let user = { name: "John", age: 30, }4 operator is an exotic guest in the code.

To walk over all keys of an object, there exists a special form of the loop: // this would give a syntax error user.likes birds = true5. This is a completely different thing from the // this would give a syntax error user.likes birds = true6 construct that we studied before.

The syntax:

// get property values of the object: alert( user.name ); // John alert( user.age ); // 306

For instance, let’s output all properties of user.isAdmin = true;8:

// get property values of the object: alert( user.name ); // John alert( user.age ); // 307

Note that all “for” constructs allow us to declare the looping variable inside the loop, like // this would give a syntax error user.likes birds = true8 here.

Also, we could use another variable name here instead of user.isAdmin = true;3. For instance, let user = {}; // set user["likes birds"] = true; // get alert(user["likes birds"]); // true // delete delete user["likes birds"];0 is also widely used.

Are objects ordered? In other words, if we loop over an object, do we get all properties in the same order they were added? Can we rely on this?

The short answer is: “ordered in a special fashion”: integer properties are sorted, others appear in creation order. The details follow.

As an example, let’s consider an object with the phone codes:

// get property values of the object: alert( user.name ); // John alert( user.age ); // 308

The object may be used to suggest a list of options to the user. If we’re making a site mainly for a German audience then we probably want let user = {}; // set user["likes birds"] = true; // get alert(user["likes birds"]); // true // delete delete user["likes birds"];1 to be the first.

But if we run the code, we see a totally different picture:

  • USA (1) goes first
  • then Switzerland (41) and so on.

The phone codes go in the ascending sorted order, because they are integers. So we see let user = {}; // set user["likes birds"] = true; // get alert(user["likes birds"]); // true // delete delete user["likes birds"];2.

Integer properties? What’s that?

The “integer property” term here means a string that can be converted to-and-from an integer without a change.

So, let user = {}; // set user["likes birds"] = true; // get alert(user["likes birds"]); // true // delete delete user["likes birds"];3 is an integer property name, because when it’s transformed to an integer number and back, it’s still the same. But let user = {}; // set user["likes birds"] = true; // get alert(user["likes birds"]); // true // delete delete user["likes birds"];4 and let user = {}; // set user["likes birds"] = true; // get alert(user["likes birds"]); // true // delete delete user["likes birds"];5 are not:

// get property values of the object: alert( user.name ); // John alert( user.age ); // 309

…On the other hand, if the keys are non-integer, then they are listed in the creation order, for instance:

user.isAdmin = true;0

So, to fix the issue with the phone codes, we can “cheat” by making the codes non-integer. Adding a plus let user = {}; // set user["likes birds"] = true; // get alert(user["likes birds"]); // true // delete delete user["likes birds"];6 sign before each code is enough.

Like this:

user.isAdmin = true;1

Now it works as intended.

Objects are associative arrays with several special features.

They store properties (key-value pairs), where:

  • Property keys must be strings or symbols (usually strings).
  • Values can be of any type.

To access a property, we can use:

  • The dot notation: let user = {}; // set user["likes birds"] = true; // get alert(user["likes birds"]); // true // delete delete user["likes birds"];7.
  • Square brackets notation let user = {}; // set user["likes birds"] = true; // get alert(user["likes birds"]); // true // delete delete user["likes birds"];8. Square brackets allow taking the key from a variable, like let user = {}; // set user["likes birds"] = true; // get alert(user["likes birds"]); // true // delete delete user["likes birds"];9.

Additional operators:

  • To delete a property: let key = "likes birds"; // same as user["likes birds"] = true; user[key] = true;0.
  • To check if a property with the given key exists: let key = "likes birds"; // same as user["likes birds"] = true; user[key] = true;1.
  • To iterate over an object: let key = "likes birds"; // same as user["likes birds"] = true; user[key] = true;2 loop.

What we’ve studied in this chapter is called a “plain object”, or just let key = "likes birds"; // same as user["likes birds"] = true; user[key] = true;3.

There are many other kinds of objects in JavaScript:

  • let key = "likes birds"; // same as user["likes birds"] = true; user[key] = true;4 to store ordered data collections,
  • let key = "likes birds"; // same as user["likes birds"] = true; user[key] = true;5 to store the information about the date and time,
  • let key = "likes birds"; // same as user["likes birds"] = true; user[key] = true;6 to store the information about an error.
  • …And so on.

They have their special features that we’ll study later. Sometimes people say something like “Array type” or “Date type”, but formally they are not types of their own, but belong to a single “object” data type. And they extend it in various ways.

Objects in JavaScript are very powerful. Here we’ve just scratched the surface of a topic that is really huge. We’ll be closely working with objects and learning more about them in further parts of the tutorial.

Apa tanda yang dapat digunakan untuk membuat objek di javascript?

Objek pada javascript, dapat dibuat dengan tanda kurung kurawal dengan isi berupa key dan value. Kode di atas bisa juga ditulis seperti ini: var car = { type:"Fiat", model:"500", color:"white" };

Apa itu objek pada javascript?

Object adalah sekumpulan list dari tipe data primitif (terkadang juga tipe data reference) yang menyimpan nilai dengan konsep berpasangan name-value. Tiap item (yang lebih dikenal dengan nama variabel) disebut dengan property, dan function disebut dengan method.

Apa yang dimaksud dengan method pada javascript?

Methodmethod ini digunakan untuk manipulasi array, seperti menambah item baru, menghapus, dan sebagainya.

Apa itu tipe data javascript?

Tipe data adalah jenis-jenis data yang bisa kita simpan di dalam variabel. Ada beberapa tipe data dalam pemrograman Javascript: String (teks) Integer atau Number (bilangan bulat)

Postingan terbaru

LIHAT SEMUA