2

I am practicing code readability and "beautifying" my code, so I want to learn how to implement this.

I've seen other code do this, but I don't remember which or how they did it.

I want to create my own .string functions, like so:

var rmbrackets = new function() {
  return String.replace(/\[.*?\]/g, "");
}
console.log("this is a [test]test!".rmbrackets);

I want the above to log "this is a test!". The above is just an example, I want to do this with many other functions.

How can i do this?

2
  • In JavaScript, you have to put parens on when invoking a function "foo".rmbrackets(), unlike Ruby. Without the parens, you are grabbing the reference to the function instead. Commented Feb 15, 2014 at 17:43
  • I found out. Thanks for clearing it up! Commented Feb 15, 2014 at 17:47

2 Answers 2

3

Try

String.prototype.rmbrackets = function() {
  return this.replace(/\[.*?\]/g, "");
};

This adds to the Object String's prototype.

Also, you'll need to add () to actually execute it, so the full code would be

String.prototype.rmbrackets = function() {
  return this.replace(/\(.*?\)/g, "");
};
console.log("this is a (test)test!".rmbrackets());

In this example I edited your regex to match what you want to achieve.

Demo


If you agree with Matt Greer, then you could just create a function for this:

function rmbrackets(text)
{
  return text.replace(/\(.*?\)/g, "");
};
console.log(rmbrackets("this is a (test)test!"));

Because if everyone did this, built in prototypes would get polluted pretty quickly. People would step over each other a lot. There is also the problem if browsers natively added a rmbrackets, you might shadow it. Other problems include unexpected properties showing up in for in loops and such. In general, the JS community has agreed to leave built in prototypes alone.

Demo

Sign up to request clarification or add additional context in comments.

5 Comments

Just keep in mind altering global prototypes is pretty well shunned. Probably best to not do this if you intend your code to be used by others.
Oh, ok. Why though? Does this affect execution or loading speeds?
@MattGreer I don't understand why, could you explain?
@JacobPedersen if everyone did this, built in prototypes would get polluted pretty quickly. People would step over each other a lot. There is also the problem if browsers natively added a rmbrackets, you might shadow it. Other problems include unexpected properties showing up in for in loops and such. In general, the JS community has agreed to leave built in prototypes alone.
Alright, i guess i should not use this. Thanks a lot though!
1

Well... you can modify the prototype property to add new functionality:

String.prototype.sayHello = function(){
   return this + " Hello!";
}

"Blah".sayHello(); // "Blah Hello!"

2 Comments

You have some slight syntax issues, other than that this is pretty much what the OP wants
@MattGreer - Yeah, just now saw that. Typed in a hurry :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.