- How to define object ?
- How to define getter/setters ?
- How to access property ?
- How to access property with space in its definition ?
- How to define computed properties ?
- How to check if property exist in object: existence check
- How to loop over object ?
- How to iterate over object ?
- How to declare constant ?
Object Properties
- What are property flags ?
- What is Object.getOwnPropertyDescriptor(obj, propertyName) for ?
- What is Object.defineProperty ?
- What is Object.defineProperties ?
- What is Object.preventExtensions ?
- What is Object.seal(obj) ?
- What is Object.freeze(obj) ?
- What is Object.isExtensible(obj) ?
- What is Object.isSealed(obj) ?
- What is Object.isFrozen(obj) ?
Object Proptotypes
- e
- 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
Your goal is to complete the
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 location
, name
, skillz
, and station
as parameters. Then we can pass in our lighthouseRock
object as the location
and start to add rangers.- As rangers are added, increment the number of rangers at the
location
using itsnumRangers
property. - Add a property to the
location
using bracket notation that will be used to hold a ranger object. Specifically, each property will be namedranger1
,ranger2
,ranger3
, etc. This will require string concatenation and the current value of thenumRangers
property. - Now that you have your
ranger
property, assign an object literal to it that contains properties forname
,skillz
, andstation
. Then assign values to those properties using the parameters that we pass in. - Call your
addRanger
function three times with the appropriate arguments to add the following rangers, in order, to thelocation
:
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 propertylighthouseRock.numRangers ++;location["ranger" + location.numRangers] = {name: name,skillz: skillz,station: station};}// call addRanger three times to add the new rangersvar 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 propertylocation.numRangers ++;location["ranger" + location.numRangers] = {name: name,skillz: skillz,station: station};}// call addRanger three times to add the new rangersaddRanger(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
The ranger-devs want you to build a new function property for
var superBlinders = [ ["Firestorm", 4000], ["Solar Death Ray", 6000], ["Supernova", 12000] ];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
.- Create a new
addBulb
function property for thelighthouseRock
object, but don’t add it directly inside the object. Instead, add it on the outside of thelighthouseRock
object. - Pass in
name
andwattage
as parameters. - Push any new bulb to the
weaponBulbs
array. Notice how the data is arranged and indexed in thesuperBlinders
array. That means we’re going to need to push a sub-array with thename
andwattage
.
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