Wednesday, July 24, 2013

OoPs




!!!!Make your own data types!!!!


Good times creating objects!


var jose = "My length is 15"; //I am already an object and I already have built in methods and properties
document.write(jose.length);
/*document actually refers to the document window in your browser and is a built in object. In this instance it is using the method write.*/






















When creating an object it is considered good times put your constructor function in the  head of the markup, 


       <head>
<script>
function person(name, number) {
//this means we are working with the current object
this.name = name;
this.number = number;
}

//instantiation
var jose = new person("Jose", "1-800-JOSE");
var mario = new person("Super Mario", 1112223333);
</script>
</head>
<body>
<script>
document.write(mario.name);
</script>
</body>



Object Initializers are faster than Constructors


<head>
<script>
jose = {name:"Jose Mayorquin", city: "Seattle"};
mario = {name:"Super Mario", city:"Super Mario World"};
</script>
</head>
<body>
<script>
document.write(jose.name + " lives in " + jose.city + ", but wishes he lived in " + mario.city);
</script>
</body>/*However, you should only use these when you are making one or two objects. If you plan on instantiating massive objects you should use a constructor.*/
_______________________________________________________________
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
So far we have only had object properties but now I will add a method.

<head>
<script>
function person(name, score) {
/*this means we are working with the current object*/
this.name = name;
this.score = score;
this.pointsToHighscore = highScore;/*we don't need to add paranthesis tthe end because it will use the existing object properties and bust out a calulation as defined in the objects function*/
}

function highScore() {
var numPoints = 4444455555666666 - this.score;
return numPoints; //we need to return it 
}

//instantiation
var jose = new person("122333", );//I'm no good:
var mario = new person("Super Mario", 7777777);
</script>
</head>
<body>
<script>
document.write(jose.pointsToHighscore());
</script>
</body>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
Arrays are object and therefore have Properties and Methods.

No comments:

Post a Comment