简体   繁体   中英

Obj-C category/extension Swift in Javascript ES6

I'm looking to add methods to a class in Javascript ES6 without creating a subclass of it, in the same way with the categories in Objective-C or extensions in Swift.

I didn't find any information about it.

Any suggestion?

ES6 class syntax is simply syntax sugar over JavaScript's prototypal inheritance model. To add methods to a class, all you can do is add more properties onto the prototype.

class Something {

}

Something.prototype.newMethod = function(){

};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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