Created
August 2, 2014 19:13
-
-
Save pedromcunha/aed804e428a5dfe1635b to your computer and use it in GitHub Desktop.
Prototypal Methods: Creating a new inherited method in the String prototype
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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