-
-
Save defields923/2f00bd3dd196d43346dc67af406bb8a8 to your computer and use it in GitHub Desktop.
ObjectsObjects studies// source https://jsbin.com/niyoqew
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -----Objects----- // | |
/* An object, like an array, is a container for data, but an object stores key/value pairs | |
with curly braces. Almost every is considered an object except primitive values, including | |
strings, numbers, booleans, null, and undefined. Objects can be created in three ways: */ | |
"use strict"; | |
var obj1 = {}; // the literal method, creating an empty object assigned to the variable | |
console.log(obj1); | |
var obj2 = Object.create({ another: "object" }); // requires an argument to be passed into the | |
//parentheses for having obj2 inherit its methods and/or properties | |
/* To demonstrate inherittance of properties between objects, obj1 will has some key/values | |
assigned, and then obj3 will inherit them. */ | |
obj1.first = "sup"; | |
obj1.second = "yo"; | |
console.log(obj1); // no longer empty; | |
var obj3 = new Object(obj1); // a new object "obj3," with the same values as obj1! | |
console.log(obj3); | |
/* object keys and key/values can be accessed using either dot or bracket notation, though | |
care must be taken: dot notation does not require the quotation marks around a string while | |
bracket notation does. A string with a space in it will require bracket notation. Dot | |
notation is useful when you know that a specific key name exists within the object. */ | |
obj1["a string!"] = "Yay!"; | |
console.log(obj1["a string!"]); // logs "Yay!" | |
/* An object method is an object property that contains a function definition: */ | |
obj1.fun1 = function () { | |
return "Sup"; | |
}; | |
console.log(obj1.fun1); // logs the actual function definition | |
console.log(obj1.fun1()); // the parentheses cause the function attached to fun1 to execute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment