[Go to site: main page, start]

0% found this document useful (0 votes)
34 views6 pages

Understanding JavaScript Objects

The document discusses JavaScript objects. It explains that objects contain properties and methods, similar to real-life objects like cars which have properties like color and weight and methods like start and stop. It provides examples of defining objects with properties and methods, and accessing object properties and methods. It also covers the use of the this keyword in method definitions to refer to the object that owns the method.

Uploaded by

oussama
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views6 pages

Understanding JavaScript Objects

The document discusses JavaScript objects. It explains that objects contain properties and methods, similar to real-life objects like cars which have properties like color and weight and methods like start and stop. It provides examples of defining objects with properties and methods, and accessing object properties and methods. It also covers the use of the this keyword in method definitions to refer to the object that owns the method.

Uploaded by

oussama
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • JavaScript Objects Overview
  • Object Definition
  • Object Properties
  • Object Methods
  • The this Keyword
  • Accessing Object Methods

1/16/2021 JavaScript Objects

  HTML CSS MORE  EXERCISES   


[Link] LOG IN

JavaScript Objects
❮ Previous Next ❯

Real Life Objects, Properties, and Methods


In real life, a car is an object.

A car has properties like weight and color, and methods like start and stop:

Object Properties Methods

[Link] = Fiat [Link]()

[Link] = 500 [Link]()

[Link] = 850kg [Link]()

[Link] = white [Link]()

All cars have the same properties, but the property values differ from car to car.

All cars have the same methods, but the methods are performed at different times.

JavaScript Objects
[Link] 1/11
1/16/2021 JavaScript Objects

You have already learned that JavaScript variables are containers for data values.
  HTML CSS MORE  EXERCISES   
This code assigns a simple value (Fiat) to a variable named car:

var car = "Fiat";

Try it Yourself »

Objects are variables too. But objects can contain many values.

This code assigns many values (Fiat, 500, white) to a variable named car:

var car = {type:"Fiat", model:"500", color:"white"};

Try it Yourself »

The values are written as name:value pairs (name and value separated by a colon).

JavaScript objects are containers for named values called properties or methods.

Object Definition
You define (and create) a JavaScript object with an object literal:

Example
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

[Link] 2/11
1/16/2021 JavaScript Objects

 Try
it Yourself
HTML » CSS MORE  EXERCISES   

Spaces and line breaks are not important. An object definition can span multiple lines:

Example

var person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};

Try it Yourself »

Object Properties
The name:values pairs in JavaScript objects are called properties:

Property Property Value

firstName John

lastName Doe

age 50

eyeColor blue

Accessing Object Properties


You can access object properties in two ways:

[Link] 3/11
1/16/2021 JavaScript Objects

  HTML CSS MORE  EXERCISES   


[Link]

or

objectName["propertyName"]

Example1
[Link];

Try it Yourself »

Example2
person["lastName"];

Try it Yourself »

Object Methods
Objects can also have methods.

Methods are actions that can be performed on objects.

Methods are stored in properties as function definitions.

Property Property Value

firstName John

[Link] 4/11
1/16/2021 JavaScript Objects

lastName Doe
  HTML CSS MORE  EXERCISES   
age 50

eyeColor blue

fullName function() {return [Link] + " " + [Link];}

A method is a function stored as a property.

Example
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return [Link] + " " + [Link];
}
};

The this Keyword


In a function definition, this refers to the "owner" of the function.

In the example above, this is the person object that "owns" the fullName function.

In other words, [Link] means the firstName property of this object.

Read more about the this keyword at JS this Keyword.

Accessing Object Methods


[Link] 5/11
1/16/2021 JavaScript Objects

You access an object method with the following syntax:


  HTML CSS MORE  EXERCISES   

[Link]()

Example
name = [Link]();

Try it Yourself »

If you access a method without the () parentheses, it will return the function
definition:

Example

name = [Link];

Try it Yourself »

Do Not Declare Strings, Numbers, and Booleans


as Objects!
When a JavaScript variable is declared with the keyword " new ", the variable is created
as an object:

var x = new String(); // Declares x as a String object


var y = new Number(); // Declares y as a Number object
var z = new Boolean(); // Declares z as a Boolean object

[Link] 6/11

Common questions

Powered by AI

JavaScript allows the creation and manipulation of objects using both object literals and constructor functions, offering flexibility similar to but more dynamic than classical inheritance seen in languages like Java or C++. Objects can be created on-the-fly through literals or with constructors for more structured instantiation, diverging from class-based instances typical in static languages . Because functions can be stored in properties as methods, JavaScript's prototype-based inheritance enables easy extension and modification of object behavior at runtime, making it highly adaptive for varied programming needs .

Object properties in JavaScript can be accessed using dot notation or bracket notation. Dot notation, such as objectName.propertyName, is straightforward and easy to read, making it ideal for property names that are valid identifiers . Bracket notation, such as objectName["propertyName"], is more flexible as it allows the use of variables or property names that contain special characters, spaces, or reserved words .

To define a JavaScript object method that concatenates two property values, first declare an object and include a method function. For example: var person = { firstName: "John", lastName: "Doe", getFullName: function() { return this.firstName + " " + this.lastName; } }; . The method 'getFullName' combines the 'firstName' and 'lastName' properties using '+' and includes a space for separation. To use this method, one would call person.getFullName(), which returns "John Doe" by accessing the properties through 'this' .

The 'this' keyword in JavaScript is used within a function to refer to the object that the function belongs to. It allows methods to access other properties and methods of the same object . For example, in a method that constructs a full name from first and last names, 'this.firstName' refers to the firstName property of the current object instance that owns the method. This contextual function behavior is essential for methods to dynamically interact with object data .

A JavaScript object literal is defined using curly braces with property name and value pairs separated by colons. Multiple properties are separated by commas and can be formatted across multiple lines for readability. For example: var person = { firstName: "John", lastName: "Doe", age: 50, eyeColor: "blue" }; . Proper formatting improves code clarity and maintainability, especially in complex objects.

Using the 'new' keyword to declare variables like strings, numbers, and booleans creates them as objects, contrary to their primitive types. This can lead to confusion and inefficient code since objects consume more memory and may have different behaviors, such as returning objects instead of primitive values when compared directly . It's advisable to declare these types without 'new' to ensure they behave as expected, retaining the benefits of primitives .

Properties and methods in JavaScript are central to the object-oriented programming paradigm because they allow data and behavior to be bundled together, forming the basis of objects. Properties serve as the object's state, while methods represent behaviors or actions that can be performed on the state . This encapsulation enables abstraction, where complex details are hidden, and interaction occurs only through well-defined interfaces, promoting modular and reusable code. By using properties and methods for encapsulation, JavaScript supports inheritance, polymorphism, and other OOP concepts, making it a flexible language for creating structured and efficient code architectures .

In JavaScript objects, properties are variables that contain data values, whereas methods are functions stored as properties used to perform actions on the object . This distinction is crucial because it defines the object's structure and functionalities: properties hold the state of the object, while methods define the behavior . Understanding this distinction allows developers to design objects that encapsulate data and methods that manipulate that data effectively, adhering to object-oriented principles.

Method calling syntax in JavaScript is crucial; using parentheses (object.method()) executes the function, while omitting them (object.method) returns the function definition rather than executing it . This distinction is important for understanding when a function is immediately invoked or when it’s being passed around for later execution. Failing to include parentheses when desired can lead to bugs where functions are not executed, instead being stored or referenced, such as unintentionally passing function references where function results are expected .

JavaScript methods provide functionality that extends beyond simple data storage, allowing objects to perform tasks and manipulate their data. While properties store data values, methods implement actions based on these values, enabling dynamic behavior like computations from current data states or interactions between properties . For instance, a method can update several properties at once or return derived data like a full name from separate first and last names, thus enhancing the object's utility by embedding behavior within its structure .

1/16/2021
JavaScript Objects
https://www.w3schools.com/js/js_objects.asp
1/11
❮ Previous (https://www.w3schools.com/js/js_fun
1/16/2021
JavaScript Objects
https://www.w3schools.com/js/js_objects.asp
2/11
You have already learned that JavaScript variab
1/16/2021
JavaScript Objects
https://www.w3schools.com/js/js_objects.asp
3/11
Try it Yourself » (https://www.w3schools.com/js
1/16/2021
JavaScript Objects
https://www.w3schools.com/js/js_objects.asp
4/11
objectName.propertyName
or
objectName["property
1/16/2021
JavaScript Objects
https://www.w3schools.com/js/js_objects.asp
5/11
lastName
Doe
age
50
eyeColor
blue
fullName
func
1/16/2021
JavaScript Objects
https://www.w3schools.com/js/js_objects.asp
6/11
You access an object method with the following

You might also like