GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialrepublic.com — Creating and Manipulating Objects in JavaScript - Tutorial Republic JAVASCRIPT BASIC
JS Introduction
JS Getting Started
JS Syntax
JS Variables
JS Generating Output
JS Data Types
JS Operators
JS Events
JS Strings
JS Numbers
JS If…Else
JS Switch…Case
JS Arrays
JS Sorting Arrays
JS Loops
JS Functions
JS Objects
JAVASCRIPT & DOM
JS DOM Nodes
JS DOM Selectors
JS DOM Styling
JS DOM Get Set Attributes
JS DOM Manipulation
JS DOM Navigation
JAVASCRIPT & BOM
JAVASCRIPT ADVANCED
JS Date and Time
JS Math Operations
JS Type Conversions
JS Event Listeners
JS Event Propagation
JS Borrowing Methods
JS Hoisting Behavior
JS Closures
JS Strict Mode
JS JSON Parsing
JS Error Handling
JS Regular Expressions
JS Form Validation
JS Cookies
JS AJAX Requests
JS ES6 Features
JAVASCRIPT EXAMPLES
JAVASCRIPT REFERENCE
Advertisements
JavaScript ObjectsIn this tutorial you will learn how to create and use objects in JavaScript. What is an Object?JavaScript is an object-based language and in JavaScript almost everything is an object or acts like an object. So, to work with JavaScript effectively and efficiently we need to understand how objects work as well as how to create your own objects and use them. A JavaScript object is just a collection of named values. These named values are usually referred to as properties of the object. If you remember from the JavaScript arrays chapter, an array is a collection of values, where each value has an index (a numeric key) that starts from zero and increments by one for each value. An object is similar to an array, but the difference is that you define the keys yourself, such as name, age, gender, and so on. In the following sections we'll learn about objects in detail. Creating ObjectsAn object can be created with curly brackets ExampleTry this code »
The above example creates an object called The property names generally do not need to be quoted unless they are reserved words, or if they contain spaces or special characters (anything other than letters, numbers, and the ExampleTry this code »
Note: Since ECMAScript 5, reserved words can be used as object's property names without quoting. However, you should avoid doing this for better compatibility. Accessing Object's PropertiesTo access or get the value of a property, you can use the dot ( ExampleTry this code »
The dot notation is easier to read and write, but it cannot always be used. If the name of the property is not valid (i.e. if it contains spaces or special characters), you cannot use the dot notation; you'll have to use bracket notation, as shown in the following example: ExampleTry this code »
The square bracket notation offers much more flexibility than dot notation. It also allows you to specify property names as variables instead of just string literals, as shown in the example below: ExampleTry this code »
Looping Through Object's PropertiesYou can iterate through the key-value pairs of an object using the ExampleTry this code »
Setting Object's PropertiesSimilarly, you can set the new properties or update the existing one using the dot ( ExampleTry this code »
Deleting Object's PropertiesThe ExampleTry this code »
Note: The Calling Object's MethodsYou can access an object's method the same way as you would access properties—using the dot notation or using the square bracket notation. Here's an example: ExampleTry this code »
Manipulating by Value vs. ReferenceJavaScript objects are reference types that mean when you make copies of them, you're really just copying the references to that object. Whereas primitive values like strings and numbers are assigned or copied as a whole value. To better understand all this, let's check out the following example: ExampleTry this code »
In the above example, we have made a copy of a variable ExampleTry this code »
You can clearly see, any changes made to the variable Advertisements
Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |