javascript - Is this bad practice: getMyObject().method() -


suppose have function returns object :

getmyobject(){    //do stuff here    return object; } 

is bad practice call method (that doesn't return anything) on function name itself:

getmyobject().method(); 

instead of assigning variable return object , calling method on variable :

var returnedobject = getmyobject(); returnedobject.method(); 

i working html page has many nested frames, , have access function returns 1 of these frames. frame might used several times within other functions in script, , wondering if ok me access frame in way asked above, or if better declare global variable.

*edit: * ahh haven't gotten chance use jquery. know!

in example, method chaining the better practice imho. if function returns object, upon want call method, not need reference object after calling method, don't assign variable.

also, jquery code all time(1):

$('#foo').on('click',function(){});     /\      \\     ||       \\       ||        \\ function call returns jq object <============|                   \\                        ||                    \\call method "on" upon _|| 

(1)to clarify: not claim jq methods return object .attr() or .prop() don't. mean "all time" scenario op describes very common in jq code (function call, invoke method on returned object):

var somestring = $($('.foo').get(0)).attr('id');//tricky little bugger, :) var abool = $('#foo').prop('checked'); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -