0

Suppose this existed:

Url = {}
Url.mk=function(u,n,v) { return {url:u, body:n+'='+v} }
Url.post=function(u) { /*does stuff*/ }

and it was used sometime like:

Url.post( Url.mk('xyz.com', 'a', 1) )

and other times it was used like:

var u = Url.mk('xyz.com', 'a', 1);

Is it possible to make a toString() function? So you could do this:

console.log(  u.toString() ) //prints xyz.com?a=1

I am stuck trying to create such a prototype because (I guess) var u is just an object literal and not an object defined with new.

Is there a way to create the toString() function without needing to create an object instance? Or is there a better approach?

Note that I want to be able to use whatever approach in function arguments and in the var scope.

Update (after getting the answer):

Here are the real functions in case anyone might be interested. I've found that building urls can be cumbersome and these functions are very helpful. This is just the pertinent functions as there are more that do form serialization, form posting, and ect....

One of the main problems this functions solve is to always add a CSRF token which I save in app.hv.

Anyway...it's late and this is it for now:

Url = {}
Url.objMk=function() {
var u = {
              url:  arguments[0], 
              body: 'ha=' + app.hv + '&cb_=' + parseInt(Math.random()*99999999),
              ha:    true,
              toString: function () { return this.url + '?' + this.body;}
    }
    for(var i=1; i<arguments.length; i++) 
      if (i % 2 != 0) u.body=u.body+"&"+arguments[i] + "="+encodeURIComponent(arguments[i+1]) 
    return u 
 }


Url.objReq=function() {
var u = {
          url: arguments[0] + "?request=" + encodeURIComponent(arguments[1]), 
      body: 'ha=' + app.hv + '&cb_=' + parseInt(Math.random()*99999999),
      ha: true,
          toString: function () { return this.url + '&' + this.body; } 
    }

    for(var i=2; i<arguments.length; i++)
       if (i % 2 == 0) u.body = u.body+"&"+arguments[i]+ "="+encodeURIComponent(arguments[i+1])              
    return u 
 }


Url.obj=function() {
   if (arguments.length % 2) 
      return Url.objMk.apply(this,arguments)
   else
      return Url.objReq.apply(this,arguments)
}

Update 3

After a few hours of sleep, I came up with a way of having Url.obj() actually return an object. Not sure if anyone is reading this....anyway, here's the new version.

This is pretty cool because the when using Url.obj() there is never a need to type "new"

    Url.init = function(){
        this.url            = '' 
        this.body           = ''
        this.ha             = true
        this.nv             = function (n,v) { this.body = this.body + '&' + n + '=' + encodeURIComponent(v) } 
        this.toString = function ()    { return this.url + ((this.url.indexOf('?') == -1) ? '?':'&') + this.body } 
    }

    Url.objMk=function() {
        var u       = new Url.init()
        u.url       =   arguments[0]
        u.body  = 'ha=' + app.hv + '&cb_=' + parseInt(Math.random()*99999999)

        for(var i=1; i<arguments.length; i++) {
            if (i % 2 != 0) u.nv(arguments[i], arguments[i+1]) 
        }
        return u 
    }

    Url.objReq=function() {
        var u       = new Url.init();
        u.url       =   arguments[0] + '?request=' + encodeURIComponent(arguments[1])
        u.body  = 'ha=' + app.hv + '&cb_=' + parseInt(Math.random()*99999999)

        for(var i=2; i<arguments.length; i++) {
            if (i % 2 == 0) u.nv(arguments[i], arguments[i+1]) 
        }
        return u 
    }

    Url.obj=function() {
        if (arguments.length % 2) 
            return Url.objMk.apply(this,arguments)
        else
            return Url.objReq.apply(this,arguments)
    }

This makes it possible to create the url in 3 different ways:

    var a = Url.obj('a.com', 'a');
    var b = Url.obj('b.com', 'b', 2);
    var c = Url.obj('c.com', 'c', 3, 'c2', 4).toString()

    var d = Url.obj('d.com')
    d.nv('dd', 55)
    d.nv('ee', 66)

    console.log(a.toString())
    console.log(b.toString())
    console.log(c)
    console.log(d.toString())

Update 4

Now using prototype for toString() to save memory usage (and also for the exercise of doing it)

    Url.init = function(p){
        this.url            = p    /// Url.path(p) // write if needed
        this.body           = 'ha=' + app.hv + '&cb_=' + parseInt(Math.random()*99999999)
        this.ha             = true
    }

    Url.init.prototype.toString = function(){ return this.url + ((this.url.indexOf('?') == -1) ? '?':'&') + this.body }
    Url.init.prototype.nv = function (n,v) { this.body = this.body + '&' + n + '=' + encodeURIComponent(v) } 

    Url.objMk=function() {
        var u       = new Url.init(arguments[0])
        for(var i=1; i<arguments.length; i++) {
            if (i % 2 != 0) u.nv(arguments[i], arguments[i+1])
        }
        return u 
    }

    Url.objReq=function() {
        var u       = new Url.init(arguments[0] + '?request=' + encodeURIComponent(arguments[1]))
        for(var i=2; i<arguments.length; i++) {
            if (i % 2 == 0) u.nv(arguments[i], arguments[i+1]) 
        }
        return u 
    }

    Url.obj=function() {
        if (arguments.length % 2) 
            return Url.objMk.apply(this,arguments)
        else
            return Url.objReq.apply(this,arguments)
    }

and then to make using it a breeze:

    function U(){
        if (arguments.length % 2) 
            return Url.objMk.apply(this,arguments)
        else
            return Url.objReq.apply(this,arguments)
    }

Usage ex:

var x = U('abc.html', 'aa', 33, 'bb', 55)
console.log(x.toString())

var x = U('abc.html', 'aa', 33, 'bb', 55)

    x.ajax( function(r){
      //ajax callback (not shown in here)  
    }) 

1 Answer 1

2
Url.mk=function(u,n,v) { return {url:u, body:n+'='+v, toString: function () { return this.url + '?' + this.body; } }

should work just fine

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

1 Comment

This is working great. Thank you. I could not figure that out. Makes sense, the returned object CAN carry it's own toString function--I like it.

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.