Javascript Prototypes


// objects have secret properties that we've not seen before
// valueOf
// toLocaleString
// toString
// isPrototypeOf
// propertyIsEnumerable
// hasOwnProperty

// an objects parent is called its prototype
// a prototype is like a blueprint object for the object we are trying to create
// passing down properties is called inheritance
// ojects inherit from the object prototype

// in a similar way, arrays inherit from the array prototype
// length
// pop()
// push()
// shift()
// reverse()
// sort()
// join()
// reduce()
// slide()

// and strings inherit from the string prototype...
// length
// charAt()
// trim()
// concat()
// indexOf()
// replace()
// toUppercase()
// toLowercase()
// substring()

// and the number prototype...
// toFixed()
// toExponential()
// toPrecision()

// and also the function prototype...
// name
// bind()
// call()
// apply()

// the array, string, number and function prototypes all inherit from the object prototype
// object prototype is the ancestor

// we can also add functions to prototypes

var witch = "I'll get you, my pretty... and your little dog too!";
var scarecrow = "Some people without brains do an awful lot of talking.";
var glinda = "Be gone! Before someone drops a house on you!";
var dorothy = "There's no place like home.";
var lion = "Come on, get up and fight, you shivering junkyard!";
var wizard = "Do not arouse the wrath of the great and powerful Oz!";
var tinman = "Now I know I have a heart, because it's breaking";

String.prototype.countAll = function(letter){
    var letterCount = 0;
    for(var i = 0; i < this.length; i++){
        if(this.charAt(i).toUpperCase() == letter.toUpperCase()){
            letterCount++;
        }
    }
    return letterCount;
};

console.log(witch.countAll("o"));
By Chris Armstrong
Friday 7th March 2014

Comments

There are currently no comments

Make a comment