Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //The prototype object help to quickly add a custom property or method to an object that is reflected on all instances of it
- //Object must be created with the new keyword
- //example 1
- function backwards() {
- for (var i = this.length - 1; i >= 0; i--) {
- document.write(this.charAt(i));
- }
- }
- String.prototype.write = backwards;
- var test = new String("Hello man");
- test.write();
- //example2
- function dummy() {
- }
- var test = new dummy();
- function myProperty(name){
- this.name = name;
- }
- function myMethod() {
- console.log("Say something");
- }
- dummy.prototype.name = myProperty;
- dummy.prototype.method = myMethod;
- test.prop = "Pesho";
- console.log(test.prop);
- test.method();
Advertisement
Add Comment
Please, Sign In to add comment