Skip to main content

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]);
}
---------------------------------------------------------------------------------------------------------------------














Comments

Popular posts from this blog

Microservices Design patterns

What are microservices? Microservices - also known as the microservice architecture - is an architectural style that structures an application as a collection of services that are Highly maintainable and testable Loosely coupled Independently deployable Organized around business capabilities Owned by a small team The microservice architecture enables the rapid, frequent and reliable delivery of large, complex applications. It also enables an organization to evolve its technology stack. You are developing a server-side enterprise application. It must support a variety of different clients including desktop browsers, mobile browsers and native mobile applications. The application might also expose an API for 3rd parties to consume. It might also integrate with other applications via either web services or a message broker. The application handles requests (HTTP requests and messages) by executing business logic; accessing a database; exchanging messages with other systems; and returni...

GraphQL

What is GraphQL  API Standard invented & open-sourced by Facebook Alternative to  REST API  enables declarative data fetching  exposes single endpoint & responds to queries How it works?  Why Graphql? Improvises performance by reducing the data that is to be transferred over the internet Variety of different frontend frameworks and platforms on client-side Fast development speed & expectation for rapid feature development Why Graphql is better than REST? Flexibility & efficient  No more over /under fetching of data Over fetching : Under fetching: Insightful analytics  Schema serves as contract between client and server CORE CONCEPTS : SDL :SCHEMA DEFINITION LANGUAGE Writing Data with mutations 3 kinds of mutations creating new data updating existing data deleting existing data

Jackson

<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> jackson-core </artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> jackson-annotations </artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> jackson-databind </artifactId> <version>2.9.6</version> </dependency> CBOR encoded data with Jackson <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-cbor</artifactId> <version>2.9.6</version> </dependency> In order to read and write MessagePack encoded data <dependency> <groupId>org.msgpack</groupId> <artifactId>jackson-dataformat-msgp...