0

I am trying to create a prototype of the ajax object.

I have created these functions:

$.extend({
    ajax_prototype : function(parammeters){
        instance = this;
        instance.cache = false;
        instance.crossDomain = false;
        instance.dataType = 'json';
        instance.timeout = 30000;
        instance.error = default_error_function;

        $.each(parammeters,function(key,value){
            instance[key] = value;
        });
    },
    set_ajax_action : function(template,action,func,def){
        template[action] = func;                        
    }
});

ajax_prototype
Is a constructor for the object.
Sets some default settings and some defined based on every need.

set_ajax_action
Sets the function to be executed on each event.

When I create an object like this:

temp1 = new $.ajax_prototype({
   'type'           : 'post',
   'url'            : 'controller.php',
});

I get this object:

Object { cache: false, crossDomain: false, dataType: "json", timeout: 30000, error: default_error_function(), type: "post", url: "controller.php", success: function () }

But after I use this:

$.set_ajax_action(temp1,'error',function(){console.log();});

The object becomes like this:

Object { cache: false, crossDomain: false, dataType: "json", timeout: 30000, error: function (), type: "post", url: "controller.php", success: function () }

Basicly their difference is the way error function is set. Both objects work prety good.
But I would like to make the prototype to create the object with the second form.
Can someone explain me why the difference on the two objects and how to resolve my problem?

Edit 1
I can also create the second object even if I remove the error property from my prototype and call $.set_ajax_action(...) .
My problem is why there is difference to the functions presentation to console.
I know my question is trivial and that either way the result would be the same, but I wan to know how it works.

By the way, even if I set the error property like this:

instance.error = function(){ ... };

The result will be:

Object { cache: false, ifModified: false, processData: true, crossDomain: false, dataType: "json", timeout: 30000, error: .ajax_prototype/instance.error(), url: "test" }
12
  • Why are you using jQuery.extend? You are not merging any objects with it. Commented Sep 15, 2015 at 23:19
  • @Ozan For no apparent reason. I could use plain functions. The result is the same. Commented Sep 15, 2015 at 23:21
  • 2
    I don't understand what "the way error function is set" means. Commented Sep 16, 2015 at 13:20
  • 1
    I don't understand the issue, you mean with how the function is/isn't displaying? Do you want it to display, or not want it to display? If you want it to, name the anonymous function you send it. If you don't, wrap the default in an anonymous function. Commented Sep 16, 2015 at 18:06
  • 1
    @DaveNewton in firefox this still isn't that good, it shows error: .ajax_prototype/instance.error(). Interestingly, I cannot find a way to make it display just function(). Commented Sep 16, 2015 at 18:08

1 Answer 1

1

Console is able to trace if a function can be identified somehow. For example, if it has a name or it is assigned to variable, console will show its/variable's name. If it's created inside a function, console will show it. Example:

(function testt(){
    $.set_ajax_action(temp1,'error',function(){console.log();});
})()
console.log(temp1)

this code will produce error: testt/<() (firefox).

You can hide name of function, not giving your default handler a name. For example, like this:

(function(default_error_function){
    $.extend({
        ajax_prototype : function(parammeters){
            instance = this;
            ...
            instance.error=default_error_function
            ...
        },
        set_ajax_action : ...
    });
})(function() {/* default error handler */})

Here, scope of default_error_function symbol is not global, therefore console does not show it. At the same time, handler was created outside any other function, so console only has function () to show.

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

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.