[Javascript] property function && Enumeration

var vehicle3 = {
  type: "Submarine", capacity: 8, storedAt: "Underwater Outpost",
  ranger1: { name: "Gregg Pollack", skillz: "Lasering", dayOff: "Friday"},
  ranger2: { name: "Bijan Boustani", skillz: "Roundhouse Kicks", dayOff: "Tuesday"},
  ranger3: { name: "Ashley Smith", skillz: "Torpedoing", dayOff: "Friday"},
  ranger4: { name: "Mark Krupinski", skillz: "Sniping", dayOff: "Wednesday"},
  numRangers: 4
};

function relieveDuty(vehicle, day){
  var offDuty = [];
  var onDuty = [];
  var number = vehicle["numRangers"];
  for(var i = 1; i <= number; i++){
    if(vehicle["ranger"+i]["dayOff"]==day){
        offDuty.push(vehicle["ranger"+i]);
      delete vehicle["ranger"+i];
      vehicle["numRangers"]--;
    }else{
        onDuty.push(vehicle["ranger"+i]);
      delete vehicle["ranger"+i];
    }
   
  }
    if(onDuty.length > 0){
      for(var j = 0; j < onDuty.length; j++){
vehicle["numRangers"]++;        vehicle["ranger"+vehicle["numRangers"]] = onDuty[j];
        }    
    }  return offDuty;
}

relieveDuty(vehicle3, "Friday");

 

Enumeration:

var rockSpearguns = {
  Sharpshooter: {barbs: 2, weight: 10, heft: "overhand"},
  Pokepistol: {barbs: 4, weight: 8, heft: "shoulder"},
  Javelinjet: {barbs: 4, weight: 12, heft: "waist"},
  Firefork: {barbs: 6, weight: 8, heft: "overhand"},
  "The Impaler": {barbs: 1, weight: 30, heft: "chest"},
  listGuns: function(){
  for(var property in this){
    if(this[property]["heft"] != undefined){
      console.log("Behold! " + property +
                  ", with " + this[property]["heft"] +
                  " heft!");    
    }

  }
}
};
rockSpearguns["listGuns"]();

 

你可能感兴趣的:([Javascript] property function && Enumeration)