-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
definition of "method" #2846
Comments
I’d say a method is any function-valued property that pays attention to its receiver, whether static or instance. |
Could you rephrase that in terms that the spec defines? |
Which term there does the spec not define? receiver is “this value”, static means “on a constructor”, instance means “on a prototype object”. |
It doesn't define:
Okay, so why not use those replacements/expansions instead? |
Sure: A method is a function stored in any object property whose behavior changes based on its |
Much better. The "stored in" phrase is unusual for the spec. The reader would probably understand that the function in question can be the In practice, it looks like the spec never refers to accessor functions as "methods". (And that's not due to a #2592 normalization, it goes way back.) I'm guessing that's intentional rather than an oversight, so I'd suggest changing:
to something like:
Also, I'd suggest replacing:
to:
(My thinking is that "behavior" refers to the totality of a function's input/output effect, which doesn't change.) So that would be: (Either way, it's a bit odd that the "whose" seems to attach to "property", whereas it actually attaches to "function". But I don't think that requires fixing.) |
Sounds good; your corrections still read clearly to me! |
I think you're overcomplicating things. If a child asks their parent what 'rain' is, the parent may say that it's 'fluid water that falls from the clouds'. While rain is water, an important aspect is the relationship between that water and the clouds. Whether a function depends on its receiver is irrelevant when speaking about methods. I remember that in the past you wouldn't be able to call Regarding accessor properties, I think that's also irrelevant, because from a property relationship perspective the distinction is only relevant in terms of the property attributes. In the context of methods (when we consider the relationship between objects and functions), data and accessor properties are the same with respect to the result of the |
In the console example, console.log isn’t a method anymore - but it used to be. Now it’s just a function in a namespace. |
Wondering if it'd complicate definitions to refer to things like |
You're using your own discretionary definition of the word 'method' in contradiction with the spec. |
@raulsebastianmihaila the spec's definition isn't what's actually important tho - the colloquial one is, and it largely matches the definition I've provided. |
One traditional and very familiar to experienced JS programmers is this style of creating objects: function Car() {
let speed = 0;
let accelerationRate = 5;
return {
get speed() {
return speed;
},
accelerate() {
speed += accelerationRate;
},
decelerate() {
speed -= accelerationRate;
}
};
}
const car = new Car();
car.accelerate();
car.accelerate();
car.accelerate();
car.decelerate();
console.log(car.speed); Colloquially, and also officially, |
To be fair tho, that module pattern was popular so long ago that there is an entire cohort of very experienced JS developers that have never used it. In this case I'd actually say they aren't methods - specifically, they can be destructured off and called bare, and they'll still work. They're just pretending to be methods, but they're really just namespaced functions. |
That's very relative, for instance I wouldn't be surprised if those very experienced JS developers you speak of have never opened the spec. Anyway, would you agree that when that pattern was popular, As to destructuring, there's an obvious reason not to do that: const car1 = new Car();
const car2 = new Car();
car1.accelerate();
car2.accelerate(); is much better than const {accelerate: accelerate1} = new Car();
const {accelerate: accelerate2} = new Car();
accelerate1();
accelerate2(); |
Sure. That doesn't mean the spec needs to adhere to a definition that includes an obsolete practice, though. |
I would consider And I wouldn't consider that pattern as obsolete (however we prefer to name these functions "makers", and avoid calling them with |
I don't agree that it's obsolete. It has advantages over post-ES5 classes for instance:
|
Is this the position of TC39? |
However, your "methods" aren't shared between instances - you have N copies of each function when you have N instances. That pattern was a hack around not having a good declarative way to create a constructor and prototype - |
Sorry for the confusion, no. This is our coding style at Agoric. |
You can say it's not a perfect pattern, however nowadays people go even further and they create N copies of functions with React hooks on each rerender. I think the sense in which you use the word 'obsolete' is more like 'not trendy', which I think is a strange way of looking at things, given that the pattern has important semantic advantages. |
is a property containing a function |
4.4.37 defines "method" as "function that is the value of a property", which includes just about every intrinsic function. So that definition should probably change.
Originally posted by @jmdyck in #2576 (comment)
We could say "function that is the value of a property of a prototype object". That wouldn't handle
[Typed]Array.{from,of}
, which don't satisfy that definition, but are described as methods. Still, it's pretty close.The text was updated successfully, but these errors were encountered: