Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Objects: Key / Value Pairs
- *
- * Collection
- */
- // {} is an empty literal object
- console.log(typeof {}); // -> 'object'
- var exampleObj = {
- aKey: 'a value',
- anotherKey: 'another value!'
- };
- // accessing values through DOT syntax
- console.log(exampleObj.aKey); // -> 'a value'
- console.log(exampleObj.anotherKey); // -> 'another value!'
- // objects are associative arrays
- // the key is associated with the value
- var user = {
- firstName: 'Jane',
- lastName: 'Doe'
- };
- // keys are strings...
- // accessing values through ARRAY notation/BRACKET notation
- console.log(exampleObj['aKey']); // -> 'a value'
- console.log(exampleObj['anotherKey']); // -> 'another value!'
- // adding keys
- user.age = 22;
- user.petName = 'Jax';
- console.log(user); // -> {firstName: 'Jane', lastName: 'Doe', age: 22, petName: 'Jax'}
- // deleting keys
- delete user.petName;
- console.log(user); // -> {firstName: 'Jane', lastName: 'Doe', age: 22}
- // get all the keys in an object
- var userKeys = Object.keys(user); // -> ['firstName', 'lastName', 'age']
- // userKeys.length will give the 'length' of an object
- // objects can hold anything
- var anything = {
- ayo: 'egad',
- pi: 3.14592,
- no: false,
- yeye: {},
- kk: function() { console.log('kk'); },
- lol: []
- };
- // looping over an object's keys
- for(var properyName in user) {
- console.log('key: ' + properyName, 'value in key: ' + user[properyName]);
- console.log("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement