You can do what you describe - a JavaScript object is just a collection of properties, some of which are functions. Therefore, if you can create:
String.prototype.camelize = function () { return this.replace (/(?:^|[-])(\w)/g, function (, c) { return c ? c.toUpperCase () : ''; }); };
Then you can equally call:
String.prototype.camelize = null;
Once you have done this, your camelize will be destroyed.
Now I’m sure that you have good reasons for wanting to do it this way, but I can’t imagine a situation where what you are proposing is the right thing. To do something like this in a more idiomatic way, you can create a type that wraps the string and offers the camelize function, or you can just bind this function to instances of your class - you can even refuse it from the library of utility functions in your class. Throw functions are one of the powerful features of JavaScript.
Keep in mind that your aversion to utility functions will force you to take many long shortcuts if you write JavaScript. You may need to put some of your preconceptions on one side until you come across the basics of the language (which some might call the “Good Details”), and then redefine them when you have a stronger pen. This does not mean that you cannot write the correct code in JavaScript, but it does not look like other languages that you used, and you need to conform to your practices and philosophy of the language, and not try to force the language in the form of your preconceptions. Everyone does it first , and JavaScript is so flexible that it almost works, but learning how to use it in a more idiomatic way gets big rewards as you continue.
source share