Friday, December 22, 2017

JS Concepts : All about Objects


  1. How to define object ?
  2. How to define getter/setters ?
  3. How to access property ?
  4. How to access property with space in its definition ?
  5. How to define computed properties ?
  6. How to check if property exist in object: existence check
  7. How to loop over object ?
  8. How to iterate over object ?
  9. How to declare constant ?
Object Properties
  1. What are property flags ?
  2. What is Object.getOwnPropertyDescriptor(obj, propertyName) for ?
  3. What is Object.defineProperty ?
  4. What is Object.defineProperties ?
  5. What is Object.preventExtensions ?
  6. What is Object.seal(obj) ?
  7. What is Object.freeze(obj) ?
  8. What is Object.isExtensible(obj) ?
  9. What is Object.isSealed(obj) ?
  10. What is Object.isFrozen(obj) ?
Object Proptotypes
  1. e
  2. f

-

Object functionality




























































































Setting getter/setters for Object














































var superBlinders = [
  ["Firelight", 4000],
  ["Solar Death Ray", 6000],
  ["Supernova", 12000]
];

var lighthouseRock = {
  gateClosed: true,
  bulbs: [200, 500, 750],
  capacity: 30,
  secretPassageTo: "Underwater Outpost"
};

// remove bulbs property from lighthouseRock
delete lighthouseRock.bulbs;

// add weaponBulbs property to lighthouseRock

lighthouseRock.weaponBulbs = superBlinders;
// log the correct weaponBulbs array value to the console
console.log(lighthouseRock.weaponBulbs[2][0]);
------------------------------------------------------------------------------------------------------------------------

To the Lighthouse, Quick!

PIRATES AHOY! It’s time for the ranger-devs to get over to the Lighthouse and throw down! Our lighthouseRock object now has a new numRangers property to track how many rangers are fighting at the Lighthouse.
Your goal is to complete the addRanger function that takes in locationnameskillz, and station as parameters. Then we can pass in our lighthouseRock object as the location and start to add rangers.
  1. As rangers are added, increment the number of rangers at the location using its numRangers property.
  2. Add a property to the location using bracket notation that will be used to hold a ranger object. Specifically, each property will be named ranger1ranger2ranger3, etc. This will require string concatenation and the current value of the numRangers property.
  3. Now that you have your ranger property, assign an object literal to it that contains properties for nameskillz, and station. Then assign values to those properties using the parameters that we pass in.
  4. Call your addRanger function three times with the appropriate arguments to add the following rangers, in order, to the location:
name: "Nick Walsh", skillz: "magnification burn", station: 2
name: "Drew Barontini", skillz: "uppercut launch", station: 3
name: "Christine Wong", skillz: "bomb defusing", station: 1
var lighthouseRock = {
var lighthouseRock = {
  gateClosed: true,
  weaponBulbs: superBlinders,
  capacity: 30,
  secretPassageTo: "Underwater Outpost",
  numRangers: 0
};
function addRanger(location, name, skillz, station) {
  // increment the number of rangers property
 lighthouseRock.numRangers ++;  
  location["ranger" + location.numRangers] = {
    name: name,
    skillz: skillz,
    station: station
  };
}
// call addRanger three times to add the new rangers
var ranger1 = addRanger(lighthouseRock,"Christine Wong", skillz, 1);
var ranger2 =addRanger(lighthouseRock, "Nick Walsh", "magnification burn", 2);
var ranger3 =addRanger(lighthouseRock, "Drew Barontini", "uppercut launch", 3);


var superBlinders = [ ["Firestorm", 4000], ["Solar Death Ray", 6000], ["Supernova", 12000] ];
var lighthouseRock = {
  gateClosed: true,
  weaponBulbs: superBlinders,
  capacity: 30,
  secretPassageTo: "Underwater Outpost",
  numRangers: 0
};
function addRanger(location, name, skillz, station) {
  // increment the number of rangers property
 location.numRangers ++;  
  location["ranger" + location.numRangers] = {
    name: name,
    skillz: skillz,
    station: station
  };
}
// call addRanger three times to add the new rangers
addRanger(lighthouseRock,"Christine Wong",1);
addRanger(lighthouseRock, "Nick Walsh", "magnification burn", 2);
addRanger(lighthouseRock, "Drew Barontini", "uppercut launch", 3);
object functionality

var superBlinders = [ ["Firestorm", 4000], ["Solar Death Ray", 6000], ["Supernova", 12000] ];

var lighthouseRock = {
  gateClosed: true,
  weaponBulbs: superBlinders,
  capacity: 30,
  secretPassageTo: "Underwater Outpost",
  numRangers: 3,
  ranger1: {name: "Nick Walsh", skillz: "magnification burn", station: 2},
  ranger2: {name: "Drew Barontini", skillz: "uppercut launch", station: 3},
  ranger3: {name: "Christine Wong", skillz: "bomb defusing", station: 1},
  addRanger:function( name, skillz, station){
    this.numRangers++;
  this["ranger" + this.numRangers] = {
    name: name,
    skillz: skillz,
    station: station
  };
  
  }

};



Functions as Properties II

It looks like ranger4 has a station assignment at Lighthouse Rock, but now there aren’t enough super-blinding bulbs for everyone.
The ranger-devs want you to build a new function property for lighthouseRock that adds a new super-blinding bulb to the weaponBulbs array, complete with a name and insane wattage.
  1. Create a new addBulb function property for the lighthouseRockobject, but don’t add it directly inside the object. Instead, add it on the outside of the lighthouseRock object.
  2. Pass in name and wattage as parameters.
  3. Push any new bulb to the weaponBulbs array. Notice how the data is arranged and indexed in the superBlinders array. That means we’re going to need to push a sub-array with the name and wattage.
var superBlinders = [ ["Firestorm", 4000], ["Solar Death Ray", 6000], ["Supernova", 12000] ];

var lighthouseRock = {
  gateClosed: true,
  weaponBulbs: superBlinders,
  capacity: 30,
  secretPassageTo: "Underwater Outpost",
  numRangers: 3,
  ranger1: {name: "Nick Walsh", skillz: "magnification burn", station: 2},
  ranger2: {name: "Drew Barontini", skillz: "uppercut launch", station: 3},
  ranger3: {name: "Christine Wong", skillz: "bomb defusing", station: 1},
  ranger4: {name: "Jordan Wade", skillz: "dual-wield hand crossbow", station: 4},
  addRanger: function(name, skillz, station) {
    this.numRangers++;
    this["ranger" + this.numRangers] = {
      name: name,
      skillz: skillz,
      station: station
    };
  }
};

// create addBulb function property here
lighthouseRock.addBulb = function(name,wattage){
  lighthouseRock.weaponBulbs.push([name,wattage]);
}
---------------------------------------------------------------------------------------------------------------------














No comments:

Post a Comment