CourtneyJordan
6/1/2015 - 2:06 PM

Object Property definitions and examples.

Object Property definitions and examples.

###Prototype


In general, if you want to add a method to a class such that all members of the class can use it, use the following syntx to extend the prototype.

className.prototype.newMethod =
function(){
  statements;
};

Example:

function Dog (breed) {
  this.breed = breed;
};
// here we make buddy and teach him how to bark
var buddy = new Dog("golden Retriever");
Dog.prototype.bark = function() {
  console.log("Woof");
};
buddy.bark();
// here we make snoopy
var snoopy = new Dog("Beagle");
/// this time it works!
snoopy.bark();

###Object Properties

hasOwnProperty()

The hasOwnProperty() method returns a boolean that indicates if the object has a specified propety and value. If the property is non-existent then no value will populate.

Example

//create a varible with the following properties and values.
var suitcase = {
    shirt: "Hawaiian"
};
//If suitcase has shorts print value
if(suitcase.hasOwnProperty("shorts")){
  console.log(suitcase.shorts); 
}else{
  suitcase.shorts = "break";
  console.log(suitcase.shorts);
}