4 ms·
javascript function does too much. It is both method and closure. Which is not universal - io has method and block, ruby has def and block And it is construct
by sergeykish 7y ago
javascript function does too much.
It is both method and closure. Which is not universal - io has method and block, ruby has def and block
And it is constructor. So it can be called both with and without `new` with different results. Other can't:
arrow = () => {}
// new arrow()
// TypeError: arrow is not a constructor
object = {
shorthand() {},
['computed']() {},
get getter() {}
}
// new object.shorthand()
// TypeError: object.shorthand is not a constructor
// new object.computed
// TypeError: object.computed is not a constructor
// getter = Object.getOwnPropertyDescriptor(object, 'getter').get
// new getter
// TypeError: getter is not a constructor
class Class {
static staticMethod () {}
method () {}
get getter() {}
}
// Class()
// TypeError: Class constructor Class cannot be invoked without 'new'
// new Class.staticMethod
// TypeError: Class.staticMethod is not a constructor
// klass = new Class
// new klass.method
// TypeError: klass.method is not a constructor
// getter = Object.getOwnPropertyDescriptor(Class.prototype, 'getter').get
// new getter
// TypeError: getter is not a constructor
function* generator() {}
// TypeError: generator is not a constructor
// new generator()
I actually was surprised just how many ways TC39 introduced so we do not use function. The change you observe is not a coincidence - it is carefully crafted.
- orange8 7y agoLet me get this straight: Are you saying all the changes in arrow functions, the class syntax and generator functions were all a carefully planned initiative to get rid of the "function" keyword because it does too much?
- sergeykish 7y agoYes, replace "function" usage with safe alternatives. And don't forget 'use strict' - it affected "function" too. "function" is too entrenched. It's usage as a constructor appeared even before .prototype: function Cat() { this.say = function() { return 'nyan' } } All the confusion about javascript object system is because of classes which have no name, we call them by their constructors. Give them real names: Cat = class {}.prototype // or function {}.prototype Cat.say = () => 'nyan' cat = new Cat.constructor cat.say() cat.__proto__ === Cat A little bit inconvenient... oh! it's everywhere: cat instanceof Cat.constructor Tom = class extends Cat.constructor {}.prototype And all the base classes are constructors Object = Object.prototype with their helper methods, it should be separate like Reflect and Math: Reflect.create = Object.constructor.create object = Reflect.create(Object) There is no simple answer, breaking change is not an option, let the planned obsolescence begin. And it works! Slow but steady so much done since 2012 http://brendaneich.github.com/Strange-Loop-2012/#/ http://brendaneich.github.com/Strange-Loop-2012/#/
- orange8 7y agoOk. I'm not convinced at all, we'll simply have to agree to disagree on that. And I'd love to see the codebase of a JS app that completely bans the "function" keyword. Any links or examples?
- ratww 7y agoAnecdotal if you're interested: I can't show it since it belongs to the company, but I just ran a grep in the app I architected and it has exactly 21 instances of the word "function"... some are comments, but most of the ones that aren't are inside a lodash debounce function. Total of 140000 non-blank/non-commented lines of code. Btw, it's not banned, and I never really enforced it... but since I never use it myself, I guess the others devs picked it up? It's in Vue.js, and in Vue you rarely see old-style functions anywhere, so there's that.
- orange8 7y agoThat's interesting. However, the Vue.js library itself uses the "function" keyword a lot, and the library is part of your applications code. Infact, it uses normal functions almost exclusively. source: https://raw.githubusercontent.com/vuejs/vue/dev/dist/vue.common.dev.js https://raw.githubusercontent.com/vuejs/vue/dev/dist/vue.com... Furthermore, the official Vue documentation explicitly discourage the use of arrow functions in some places, so whoever set that rule just did not know what they were doing, sorry if that sounds harsh. source: - https://vuejs.org/v2/guide/instance.html#Instance-Lifecycle-Hooks https://vuejs.org/v2/guide/instance.html#Instance-Lifecycle-... - https://stackoverflow.com/questions/43929650/vuejs-why-is-this-undefined https://stackoverflow.com/questions/43929650/vuejs-why-is-th... Saying all arrows functions are bad and evil or that all normal functions are bad and evil is nothing but dogma. Each has its own place and uses, it seems many JS devs would rather not think of this at all and just use one form everywhere.
- ratww 7y agoWe're not using arrow functions for everything. Why are you assuming this? We're using shorthand syntax in methods inside Vue.js objects [1], they also don't need the "function" keyword. In fact, they are suggested in the second link you posted, please read again. Arrow functions wouldn't even work in this situation. We're mainly using two forms: shorthand and arrow. That's how we only have 21 instances of the "function" word in our entire JS codebase, and we could remove all of them if we wanted. That's what you asked for: a "JS app that completely bans the 'function' keyword". Well, we don't ban but it's possible. And please read my comment again: I explicitly mentioned that this is not a rule, and I never enforced it. The team (39 committers ATM) reached that result organically, mostly influenced by my own personal style. And sorry if this sounds harsh, but it seems that you have extremely limited knowledge of Javascript and ES6, so I'd suggest learning before you make such claims before you make a fool of yourself. Btw, you're seem to be the one holding on to dogma here, since you seem unable to accept that the keyword is not strictly necessary in a large app. [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...