繁体   English   中英

JS:如果每个函数都返回“this”作为默认值?

[英]JS: And if every function return "this" as a default value?

我有一个代码设计问题。 考虑以下代码:

thatObj.doThis().setThat().add().update();

为了允许链接,我经常写return this; ,有时,有时我会忘记这样做,然后出现错误。

在许多情况下,我不是要求特定的结果(例如thatObj.getName()thatObj.getChildren() ),而是想要做一些更新或调用 setter(例如thatObj.setName("foo")thatObj.addChild(child)thatObj.update() ),我想知道return this;是否更方便return this; 对于任何方法调用,我的意思是作为 javascript 默认行为,如果不是,不这样做的原因可能是什么。

  • 如果你不明确返回某些东西,JS 将返回undefined
  • JS 构造函数返回this除非你的构造函数返回一个对象。
  • CoffeeScript 默认返回最后一个表达式,
  • 您希望对象上的所有方法默认返回this

每个人都有自己的看法,什么是“正确”的做法。

从现在开始,JS 将始终从任何方法返回 this 是个好主意吗?

从一个时刻到另一个时刻,至少有 2/3 的网络会被破坏。 所以,告诉我,这是个好主意吗?

JS 很久以前就已经制定了规则,一些基本的东西不会改变(正如 Pointy 已经提到的)。 那么你为什么不注意这种行为:

 //extracted that from the function to avoid memory leakage function wrapFunction(fn) { return function() { let result = fn.apply(this, arguments); return result === undefined ? this : result; } } //filter === false => only own methods //filter === true => own methods and inherited methods //filter is Array => only the passed keys (if they are methods) //filter is RegExp => use the RegExp to filter the keys //filter is function => regular filterFunction function returnThisAsDefault(objectOrConstructor, filter = false) { if (objectOrConstructor !== Object(objectOrConstructor)) throw new TypeError("Passed argument must be an object or a constructor. Got ", typeof objectOrConstructor); const validKey = key => typeof proto[key] === "function" && key !== "constructor" && key !== "prototype"; let proto = typeof objectOrConstructor === "function" ? objectOrConstructor.prototype : objectOrConstructor; let filterFn = Array.isArray(filter) ? filter.includes.bind(filter) : filter === false || filter === true ? () => true : filter instanceof RegExp ? filter.test.bind(filter) : typeof filter === "function" ? filter : () => false; let wrapped = {}; for (let p = proto, done = new Set(["constructor", "prototype"]); p != null && p !== Object.prototype;) { for (let key of Object.getOwnPropertyNames(p)) { if (typeof proto[key] !== "function" || done.has(key) || !filterFn.call(p, key)) continue; done.add(key); let d = Object.getOwnPropertyDescriptor(p, key); //typeof d.value !== "function" means that proto[key] contains a getter returning a function if (!d.writable && !d.configurable || typeof d.value !== "function") { console.log(`function ${JSON.stringify(key)} not fit to be wrapped`, d); continue; } d.value = wrapFunction(d.value); wrapped[key] = d; } if (filter === false) break; else p = Object.getPrototypeOf(p); } Object.defineProperties(proto, wrapped); return objectOrConstructor; } let thatObject = returnThisAsDefault({ doThis() { console.log("doThis()"); }, setThat() { console.log("setThat()"); }, add() { console.log("add()"); }, update() { console.log("update()"); return "success"; }, }); let result = thatObject.doThis().setThat().add().update(); console.log("result: ", result);
 .as-console-wrapper { top: 0; max-height: 100%!important }

暂无
暂无

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

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