Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

2. Working with Objects and Properties > Getting/Setting/Updating an Object’s P...

Getting/Setting/Updating an Object’s Properties Using Dot Notation or Bracket Notation

We can get, set, or update an object’s properties using either dot notation or bracket notation.

Below, I demonstrate dot notation, which is accomplished by using the object name followed by a period and then followed by the property to get, set, or update (e.g., objectName.property).

Live Code

<!DOCTYPE html><html lang="en"><body><script>

// create cody Object() object
var cody = new Object();

// setting properties
cody.living = true;
cody.age = 33;
cody.gender = 'male';
cody.getGender = function() {return cody.gender;};

// getting properties
console.log(
    cody.living,
    cody.age,
    cody.gender,
    cody.getGender()
); // logs 'true 33 male male'

// updating properties, exactly like setting
cody.living = false;
cody.age = 99;
cody.gender = 'female';
cody.getGender = function() {return 'Gender = ' + cody.gender;};

console.log(cody);

</script></body></html>

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint