简体   繁体   English

JavaScript返回this.key = variable;

[英]JavaScript return this.key = variable;

Sorry if this questions been asked I have been searching for the past couple of hours and I couldn't phrase what my sitution was in a question. 抱歉,如果有人提出这个问题,我已经搜索了过去几个小时,但我无法说出我的问题所在。

simply put I want my script to be as small as and with the specified method below can this be achieved without fault. 简而言之,我希望我的脚本尽可能小,并且使用下面的指定方法可以毫无问题地实现这一目标。

Basically if i have the below: 基本上,如果我有以下内容:

function myFunction(val){
   this.key = val;
}
myFunction.prototype.changeKey = function(newVal){
   this.key = newVal;
   return this.key; 
}

that's basically an over-simplification of my script. 这基本上是我的脚本的过度简化。

But is it just as okay to do this. 但是这样做是否也可以。

myFunction.prototype.changeKey = function(newVal){
   return this.key = newVal; 
}

I am using google chrome it seems to works fine but are there any bugs/issues with this or is it 100% fine to do this. 我正在使用谷歌浏览器,它似乎可以正常工作,但是是否有任何错误/问题,或者100%可以做到这一点。 I have never come across it before!! 我从来没有遇到过它!

Assignment "returns" the assigned value, so it is absolutely okay to write the code you have. 赋值“返回”赋值,因此编写您的代码绝对可以。

Readability may suffer, however. 但是,可读性可能会受到影响。 This is the kind of step that should be taken by a minifier, not manually. 缩小器应该采取这种步骤,而不是手动进行。

It's the same as 和...一样

this.key = newVal;
return newVal;

because the expression a = b evaluates to the value of b , which is b . 因为表达式 a = b计算结果为b的值,即b

It's similar to the idea behind 这与背后的想法相似

if(a++ < 10)

The value of a changes, but it also evaluates to a before the expression, so the side-effect is used. 的值a变化,但它也计算为a表达式之前,因此使用的副作用。

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

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