Is there a way to get all methods (private, privileged, or public) of a javascript object from within? Here's the sample object:
var Test = function() { // private methods function testOne() {} function testTwo() {} function testThree() {} // public methods function getMethods() { for (i in this) { alert(i); // shows getMethods, but not private methods } } return { getMethods : getMethods } }(); // should return ['testOne', 'testTwo', 'testThree', 'getMethods'] Test.getMethods();
The current issue is the code in getMethods()
, the simplified example will return just the public methods, but not to private ones.
edit: my test code may (or may not) be overcomplicating what i'm hoping to get at. given the following:
function myFunction() { var test1 = 1; var test2 = 2; var test3 = 3; }
is there a way to find out what variables exist in myFunction()
from within myFunction()
. the pseudo-code would look like this:
function myFunction() { var test1 = 1; var test2 = 2; var test3 = 3; alert(current.properties); // would be nice to get ['test1', 'test2', 'test3'] }
Reflect is a built-in object that provides methods for interceptable JavaScript operations. The methods are the same as those of proxy handlers. Reflect is not a function object, so it's not constructible.
Description. The Reflect. get method allows you to get a property on an object. It is like the property accessor syntax as a function.
Answer: The Reflect. preventExtensions() method returns a string. Is This Question Helpful?
The technical reason why those methods are hidden is twofold.
First, when you execute a method on the Test object, "this" will be the untyped object returned at the end of the anonymous function that contains the public methods per the Module Pattern.
Second, the methods testOne, testTwo, and testThree aren't attached to a specific object, and exist only in the context of the anonymous function. You could attach the methods to an internal object and then expose them through a public method, but it wouldn't be quite as clean as the original pattern and it won't help if you're getting this code from a third party.
The result would look something like this:
var Test = function() { var private = { testOne : function () {}, testTwo : function () {}, testThree : function () {} }; function getMethods() { for (i in this) { alert(i); // shows getMethods, but not private methods } for (i in private) { alert(i); // private methods } } return { getMethods : getMethods } }(); // will return ['getMethods', 'testOne', 'testTwo', 'testThree'] Test.getMethods();
edit:
Unfortunately, no. The set of local variables aren't accessible via a single, automatic keyword.
If you remove the "var" keyword they would be attached to the global context (usually the window object), but that's the only behavior that I know of that is similar to what you're describing. There would be a lot of other properties and methods on that object if you did that, though.
From http://netjs.codeplex.com/SourceControl/changeset/view/91169#1773642
//Reflection ~function (extern) { var Reflection = this.Reflection = (function () { return Reflection; }); Reflection.prototype = Reflection; Reflection.constructor = Reflection; Reflection.getArguments = function (func) { var symbols = func.toString(), start, end, register; start = symbols.indexOf('function'); if (start !== 0 && start !== 1) return undefined; start = symbols.indexOf('(', start); end = symbols.indexOf(')', start); var args = []; symbols.substr(start + 1, end - start - 1).split(',').forEach(function (argument) { args.push(argument); }); return args; }; extern.Reflection = extern.reflection = Reflection; Function.prototype.getArguments = function () { return Reflection.getArguments(this); } Function.prototype.getExpectedReturnType = function () { /*ToDo*/ } } (this);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With