2

I am trying to write a javascript class that loads script files as they are needed. I have most of this working. It is possible to use the library with the following Syntax:

var scriptResource = new ScriptResource('location/of/my/script.js');
scriptResource.call('methodName', arg1, arg2);

I would like to add some additional syntactic sugar so you could write

var scriptResource = new ScriptResource('location/of/my/script.js');
scriptResource.methodName(arg1, arg2);

I'm almost certain that this isnt possible but there may be an inventive solution. I guess what there need to be is some sort of methodCall event. SO the following could work

ScriptResource = function(scriptLocation)
{
    this.onMethodCall = function(methodName)
    {
        this.call(arguments);
    }
}

This code is obviously very incomplete but I hope it gives an idea of what I am trying to do

Is something like this even remotely possible?

2
  • You can't write "ScriptResource = new function". Commented Jan 9, 2009 at 11:33
  • Can we see the code for your current call() implementation - it's hard to suggest alternate solutions if we don't know how your script works... Commented Jan 9, 2009 at 12:29

3 Answers 3

3

There is a non standard method, __noSuchMethod__ in Firefox that does what you're looking for
have a look at
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/noSuchMethod

so you could define

obj.__noSuchMethod__ = function( id, args ) {
    this[id].apply( this, args );
}
Sign up to request clarification or add additional context in comments.

4 Comments

I was going to post the same answer: +1
I've just about managed to get this to work. It would be nice if there was a full cross browser solution though.
Really not sure how cross browser it is - may work in other mozilla based browsers? I've only used it in our office which is all firefox Anyone else know? Christoph?
@meouw: it's not cross browser at all (only works in Geckos); that's why I asked for your current implementation of call() so that we might look for workarounds...
0

If the set of method names is limited, then you could generate those methods:

var methods = ["foo", "bar", "baz"];
for (var i=0; i<methods.length; i++) {
    var method_name = methods[i];
    WildCardMethodHandler[method_name] = function () {
        this.handleAllMethods(method_name);
    };
}

edit: Posted this answer before the question changed dramatically.

3 Comments

I'm afriad this isn't going to work. I need to call any method. I have made a big edit to show the usage I am after.
I almost thought I answered to the wrong question. You have made some truly extensive edits.
Yes. Sorry for the change. I thought it was best to present the question in more concrete terms. I saved edit as your saved you answer. Thanks for having a go at it though.
0

An intermediary solution might be to have syntax such as:

var extObj = ScriptResource('location/of/my/script.js');  
extObj('methodname')(arg1,arg2);  

the code might look like this:

function ScriptResource(file) {
  return function(method) {
    loadExternalScript(file);
    return window[method];
  }
}

All kinds of assumptions in the code above, which I'd let you figure out yourself. The most interesting, IMHO, is - in your original implementation - how do you get the proxyied method to run synchronously and return a value? AFAIK you can only load external scripts asynchronously and handle them with an "onload" callback.

1 Comment

Synchronous AJAX is definitely possible. I'm using it every day. This page explains how: hunlock.com/blogs/Snippets:_Synchronous_AJAX

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.