0

How can I call a function directly on a string?

var test = function(x){
  console.log(x);
};

test();

** The above logs 'undefined' as it should.

If I try:

"test".test();

I get:

"error"
"TypeError: \"test\".test is not a function.

But test is a function. So even if I try:

var example = "test";

example.test();

I get:

"error"
"TypeError: example.test is not a function

Any help is greatly appreciated.

3
  • 2
    It's a function, but what is its connection with an arbitrary string? How JS would know that a string does have a function with that name? Commented May 19, 2015 at 1:23
  • To be clear: If you are trying to pass an argument to a user defined function, see Thilo's answer. If you are looking to add a method to the String object and then call that method on a String type value, see Luke's answer. Commented May 19, 2015 at 1:45
  • Yes, test (the global variable) is a function, but example.test or "test".test (the string properties) are not, as your error messages state. Commented May 19, 2015 at 6:16

5 Answers 5

3

To expand on the other answers a bit, you've created a function called test() but that function doesn't belong to any objects, and it doesn't belong to any Strings. You haven't defined a function called test() for Strings, hence the error.

To do that, you can try the following:

String.prototype.test = function() {
    console.log(this);
};
"hello".test();

Read up here on prototypes. Essentially, objects in javascript have properties and methods, and they also have the properties and methods of their prototypes.

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

5 Comments

True, although it's my understanding that the OP is just messing around with functions. I may be incorrect?
I don't think you're incorrect in your assessment of OP, but I was just pointing that out because you said "..doesn't belong to any objects"
Thank you. This is exactly what I'm looking for. I tried searching for this answer but without knowing about prototypes it was hard to find what I was looking for. I appreciate the help.
Ah. I merely meant that the function that was defined right there is kinda floating, not that there was no function named test in existence :)
1

Yes, test is a function but String.prototype.test is not a function. The value is undefined and invoking an undefined value using invocation operator throws a TypeError.

Comments

1

The question is a bit unclear, but if you are trying to pass a String as your argument to the function, that would be

 test("hello");

 var example = "hello";
 test(example);

2 Comments

It’s perfectly clear to me: he asks how to call a prototype function, or method.
Not so sure about that. Could be just basic question about how to call a function with parameters. It looks like that x parameter should be set to the String in question. In languages with unified call syntax, a.test() is the same as test(a) or test a.
0

you have to define function test() on the string first using Prototypes

For example:

String.prototype.cleanSpaces = function() {
     return this.replace(" ", '');
};

now you can call "test ".cleanSpaces()

Comments

0

use prototype

 String.prototype.test = function () {
    console.log(this.toString());
}

'test'.test();

it would ouput test in console

Comments

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.