node.js 扩展c++
As someone who was on the legendary MooTools JavaScript team, I have some affection for extending the prototypes of native objects. Of course the practice of extending prototypes is taboo these days as browser vendors are iterating and implementing new specs more quickly than the IE6 era, but extending natives in Node.js could be considered safer as, in theory, we have more control over the environment (Node.js version).
作为传奇的MooTools JavaScript小组的成员,我对扩展本地对象的原型很感兴趣。 当然,如今扩展原型的做法是忌讳的,因为浏览器供应商比IE6时代更快地迭代和实施新规范,但是在Node.js中扩展本机可以被认为更安全,因为从理论上讲,我们对环境有更多的控制权(Node.js版本)。
Extending a native within Node.js is fairly simple:
在Node.js中扩展本机非常简单:
// Inside a file module called "String.prototype.startsWith.js" // Naming convention important for maintenance and clearness of intent // This is a very, very simple shim not made for production use // It's simply to illustrate the prototype extension // More logic should be added for edge cases if(!String.prototype.startsWith) { String.prototype.startsWith = function(term) { return this.substr(0, term.length) === term; }; } // ---------- // Inside any other module that wants to use the extension require('String.prototype.startsWith'); // Usage if(myString.startsWith('Moo')) { // ... }As long as you require the the module that contains the extension code, the native will have its desired additional method. Of course this doesn't just apply to natives, you can extend other Objects in this same fashion. If you scour npm you can find loads of prototype extensions, one being String.prototype.startsWith, which also work in client side code.
只要您需要包含扩展代码的模块,本机将具有其所需的其他方法。 当然,这不仅适用于本机,还可以以相同的方式扩展其他对象。 如果您搜索npm,则可以找到大量的原型扩展,其中一个就是String.prototype.startsWith ,它也可以在客户端代码中使用。
I presume I'll receive some haterade for suggesting this practice being more OK with Node.js so have at me!
我想我会因为建议这种做法对Node.js更好而受到反感。
翻译自: https://davidwalsh.name/extend-prototypes
node.js 扩展c++
相关资源:jdk-8u281-windows-x64.exe