3 ms·
A class, generally speaking, is a binding of functions and data. In the functional example, where does Person store the fName and lName variables? Is there an
by drchopchop 6y ago
A class, generally speaking, is a binding of functions and data.
In the functional example, where does Person store the fName and lName variables? Is there an intrinsic/hidden 'this' pointer? If functions persist data, are they really classified as functions anymore?
The message passing part isn't very relevant, as the concept of calling a function pointer via string lookup is present in many dynamic languages (Ruby, etc).
- ovis 6y ago> In the functional example, where does Person store the fName and lName variables? With a closure? And you get state hiding too. > If functions persist data, are they really classified as functions anymore? Sure, since otherwise returning a function from a function wouldn't be very interesting.
- crdrost 6y agoNo this is a really important question, and it has an answer. They are called closures. They are "environments" in which variables live, each of which can have a parent environment dictated by whatever rules there are. (Some programming languages have it where every use of curly braces creates a sub-environment; in JavaScript originally only the function keyword would introduce a sub-environment and if you write `if (something) { var x = 123; }` then `x` belongs to the parent closure and it might contain `undefined` if the branch was not triggered. Since then JS has introduced let/const which are scoped to curly brackets.) When you declare a function it still has access to the full space of variables which it was lexically defined with. If you mutate those with one function then another function which has access to that environment can see those changes, etc.
- drchopchop 6y agoGood point, I guess what I was trying to get at is that an object is more equivalent to a closure (function + data i.e. local stack environment) than just a function (which executes with input parameters). The top-voted response put that more eloquently than I did.
- lukevp 6y agoThe variables are persisted within a closure since they are in the scope of the internal function definitions. Whether each function points to the same closure value as they are changed would be based on the language semantics around the creation of the closure and whether mutability of the closure variables would propagate to the other function definitions that also have a closure around that variable (if the variables are updated by reference or value).
- chrisrhoden 6y ago> In the functional example, where does Person store the fName and lName variables? Is there an intrinsic/hidden 'this' pointer? If functions persist data, are they really classified as functions anymore? There is no intrinsic "this", the code as presented is using a closure. Closures are typically considered a standard part of functional programming.
- euiq 6y agoNote that the example in the post only works because variables in the closure can be rebound. Not every language allows you to do that.