Skip to content

Instantly share code, notes, and snippets.

@pedromcunha
Created August 2, 2014 19:13
Show Gist options
  • Select an option

  • Save pedromcunha/aed804e428a5dfe1635b to your computer and use it in GitHub Desktop.

Select an option

Save pedromcunha/aed804e428a5dfe1635b to your computer and use it in GitHub Desktop.
Prototypal Methods: Creating a new inherited method in the String prototype
String.prototype.checkFor = function (input) {
var result = 0;
for (var i = 0; i<this.length; i++) {//use this to check for the length of the actual string
this.charAt(i) == input ? result++ : false; //we can use a ternary conditional here.
}
return result;
};
console.log("This is a good string".checkFor("o")); //Now we can use the new method on a string and log it out.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment