3

If I have var foo = 'bar';, how do I get all the functions callable on foo, such as toUpperCase? It's not an object, so Object.getOwnPropertyNames(foo) doesn't work:

$ node
> var foo = 'bar';
undefined
> console.log(Object.getOwnPropertyNames(foo));
TypeError: Object.getOwnPropertyNames called on non-object
    at Function.getOwnPropertyNames (native)
    at repl:1:20
    at REPLServer.eval (repl.js:80:21)
    at repl.js:190:20
    at REPLServer.eval (repl.js:87:5)
    at Interface.<anonymous> (repl.js:182:12)
    at Interface.emit (events.js:67:17)
    at Interface._onLine (readline.js:162:10)
    at Interface._line (readline.js:426:8)
    at Interface._ttyWrite (readline.js:603:14)

katspaugh's simple, elegant solution:

> console.log(Object.getOwnPropertyNames(foo.constructor.prototype));
[ 'constructor',
  'length',
  'toLowerCase',
  ... ]
undefined
5
  • I'm pretty sure it is an object. Though the properties you are looking for might not be its "own". Have you tried something like Object.keys(foo)? Commented Apr 25, 2012 at 8:46
  • 1
    @32bitkid - It's not an object, it's a primitive string value. It will sometimes be temporarily cast to an instance of String (if you call a method of String.prototype on it for example). l0b0 - Do you want this for a general case (any type) or just for strings? Commented Apr 25, 2012 at 8:48
  • @32bitkid - As far as I know, it's just a string literal until you use it as object [ref]. I guess this question is all about getting properties from String, which is exactly what the linked question talks about. Commented Apr 25, 2012 at 8:50
  • @James_Allardice right on, sometimes my brain goes a little loopy at 4am. Insomnia wins again Commented Apr 25, 2012 at 8:51
  • I'm not sure you can do it. Although, if you enter "String.prototype" in Chrome's console. You get all the methods. Commented Apr 25, 2012 at 9:00

3 Answers 3

3

The methods you want are on the String.prototype, the prototype of all strings, ruler of Valindor.

Object.getOwnPropertyNames('baz'.constructor.prototype)
Sign up to request clarification or add additional context in comments.

3 Comments

+1, I knew there was a way! This will work for all primitive values too.
Could be Object.getOwnPropertyNames(Object(whatever)) as well.
@SomeGuy, you mean Object.getOwnPropertyNames(Object.getPrototypeOf(Object(whatever))).
1

use can use the below:

$(document).ready(function () {
    var foo = 'bar';
    var div = '';
    var ob = Object.getOwnPropertyNames(foo.constructor.prototype);
    for (var i = 0; i < ob.length; i++) {
        div += ob[i] + "<br/>";
    }
    $(selector).html(div);
});

1 Comment

That's a lot of unnecessary boilerplate code for essentially the same solution as @katspaugh. But it's valid, so +1.
-2

Use eclipse :)

Or just look here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String

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.