Skip to content

Instantly share code, notes, and snippets.

Created December 3, 2016 01:50
Show Gist options
  • Save anonymous/889ba8c9338c10a18f7263f1b7895522 to your computer and use it in GitHub Desktop.
Save anonymous/889ba8c9338c10a18f7263f1b7895522 to your computer and use it in GitHub Desktop.
Objects Objects studies // source https://jsbin.com/niyoqew
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Objects studies">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Objects</title>
</head>
<body>
<script id="jsbin-javascript">
// -----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
</script>
<script id="jsbin-source-javascript" type="text/javascript">// -----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: */
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</script></body>
</html>
// -----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