简体   繁体   English

这是不好的做法:getMyObject()。method()

[英]Is this bad practice: getMyObject().method()

Suppose I have a function which returns an object : 假设我有一个返回对象的函数:

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

Is it bad practice to call a method (that doesn't return anything) on the function name itself: 在函数名称本身上调用方法(不返回任何内容)是不好的做法:

getMyObject().method();

instead of assigning a variable to the return object and then calling the method on that variable : 而不是将一个变量赋值给返回对象,然后在该变量上调用该方法:

var returnedObject = getMyObject();
returnedObject.method();

I am working with an html page that has many nested frames, and I have access to a function that returns one of these frames. 我正在使用一个有许多嵌套框架的html页面,我可以访问一个返回其中一个框架的函数。 The frame might be used several times within other functions in my script, and I was wondering if it would be ok for me to access the frame in the way asked above, or if it would be better to declare a global variable. 这个框架可能会在我的脚本中的其他函数中多次使用,我想知道我是否可以按照上面提到的方式访问框架,或者是否更好地声明一个全局变量。

* EDIT: * Ahh I haven't gotten a chance to use jQuery. * 编辑:*啊,我没有机会使用jQuery。 Good to know! 很高兴知道!

Yes, this is perfectly OK. 是的,这完全没问题。 jQuery for example uses this as well. 例如,jQuery也使用它。 It returns objects on which you can call methods immediatley. 它返回可以调用方法immediatley的对象。 This is called chaining. 这称为链接。

In your example, method chaining is the better practice IMHO. 在您的示例中,方法链接是更好的做法恕我直言。 If a function returns an object, upon which you want to call a method, but you do not need to reference that object after calling that method, don't assign it to a variable. 如果函数返回一个对象,您希望在该对象上调用该方法,但在调用该方法后不需要引用该对象,请不要将其分配给变量。

Also, jQuery code does this all the time (1): 此外,jQuery代码做这一切的时候 (1):

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

(1)To clarify: I do not claim that all jQ methods return an object .attr() or .prop() don't. (1)澄清:我没有声称所有jQ方法都返回一个对象.attr().prop()没有。 What I mean by "all the time" is actually that the scenario the OP describes is very common in jQ code (function call, invoke method on returned object): 我所说的“所有时间”实际上是OP描述的场景在jQ代码中非常常见(函数调用,返回对象上的调用方法):

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

Usually, no. 通常,没有。 Chaining method calls like that is usually simpler, more elegant, and easier to read. 像这样的链接方法调用通常更简单,更优雅,更易于阅读。 However, there are a few cases when it's better to use a variable. 但是,有一些情况下使用变量更好。

  1. If you use a method (or chain of methods) a lot of times, you can use a variable if it makes the code cleaner. 如果您多次使用方法(或方法链),则可以使用变量,如果它使代码更清晰。
  2. If the method takes a long time to process, it's better to cache the result. 如果该方法需要很长时间来处理,则最好缓存结果。 For example, if you have some method called calculateResults() , and it pulls data from a database, that takes some time. 例如,如果您有一个名为calculateResults()方法,并且它从数据库中提取数据,则需要一些时间。 If the data doesn't change, you'll be incurring that cost for each call to the method. 如果数据没有变化,那么每次调用该方法都会产生相应的费用。 Better to store it in a variable and reuse it. 最好将其存储在变量中并重用它。
  3. If the method has side effects, you should be careful about calling it more than once. 如果该方法有副作用,您应该多注意多次调用它。 Those side-effects will be inflicted each time you call it. 每次打电话都会造成这些副作用。 Again, as an example, if you have a function like nextItem() that advances to the next item and returns it (a la Java iterators), then calling it more than intended will actually change the result. 再一次,作为一个例子,如果你有一个像nextItem()这样的函数进入下一个项目并返回它(一个Java迭代器),那么调用它比预期更多将实际改变结果。 In this case, you have no choice but store the result, since calling it more than once will produce incorrect behavior. 在这种情况下,您别无选择,只能存储结果,因为多次调用它会产生不正确的行为。

Otherwise, chain away! 否则,链条走了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM