Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
105
rated 0 times [  108] [ 3]  / answers: 1 / hits: 26153  / 11 Years ago, sat, november 9, 2013, 12:00:00

I'm trying to define a "dot function" where there are no parameters but has a . and a string or number before it like these:



.toUpperCase()

.toLowerCase()

.indexOf()

.charAt()

.substring()



You do 2..toString, not toString(2).


How do you define one of them?


More From » function

 Answers
13

Defining a dot function is easy. Here's how you can define it on a single object.



var a = {}, or a = function() {}, or a = [], etc.

a.dotFunction = function() { return 'hi'; }

console.log(a.dotFunction());


If you want to define it on all instances of a class, use prototype.



function someClass() {
}

someClass.prototype.dotFunction = function() { return 'hi'; };

console.log(new someClass().dotFunction());


You can even do this on built-in types (some, like Prototype.js, do this, though most recommended against it).



Number.prototype.dotFunction = function() { return 'hi'; };

console.log((0).dotFunction());

[#74394] Thursday, November 7, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
collinarnoldp

Total Points: 10
Total Questions: 122
Total Answers: 109

Location: Spain
Member since Thu, Dec 23, 2021
3 Years ago
;