-2

HI and thanks for taking the time. When you add a function as an accessible property of a module and expose it through the return statement. In other words, if you have an object and add a property such that "name:function(){return;}:" and expose that through the return statement of the module or object. is this an expression or a declaration? Once again thanks

I apologize I am not having a great day, no I am not taking a test but I described this incorrectly. When you export functions declared in a module, are they declarations or expressions? Thanks for your patience

jk121960
  • 719
  • 9
  • 31
  • 1
    Are you taking a test right now? – evolutionxbox Sep 03 '18 at 13:18
  • Depends, expression: `var getGoats = function(goatCount) { return goatCount * goat; }` – Mark Schultheiss Sep 03 '18 at 13:20
  • Possible duplicate of [What is the difference between a function expression vs declaration in JavaScript?](https://stackoverflow.com/questions/1013385/what-is-the-difference-between-a-function-expression-vs-declaration-in-javascrip) – Mark Schultheiss Sep 03 '18 at 13:21
  • also https://stackoverflow.com/q/336859/125981 – Mark Schultheiss Sep 03 '18 at 13:22
  • I understand when I write them in the normal flow, but when you export them are they still viewed the same way? – jk121960 Sep 03 '18 at 13:24
  • 1
    @Rajesh Nope. The function in `obj ={name:function(){return;}}` is definitely *not* a declaration – Bergi Sep 03 '18 at 13:24
  • 1
    "*When you export functions declared in a module*" - notice that this terminology refers to *declarations* in an ES6 module, like `export function name() { … }`, not to properties of "module" objects. – Bergi Sep 03 '18 at 13:25
  • Let me expand, I am writing using curry function and the intermediate function before the last value executes on two seperate instances the inner curryied function, it appears the data is getting overwritten or changed and I am attempting to find out why. the first instance operates normally but the second one has mangled data – jk121960 Sep 03 '18 at 13:27
  • 1
    @jk121960 I don't think that has anything to do with expression vs declaration. You might want to [ask a new question](https://stackoverflow.com/questions/ask) with the code that does not work – Bergi Sep 03 '18 at 13:30
  • Ok thanks, I am just going through different manipulations attempting to see what the problem is. A localized simplistic test works fine but in practice is failing – jk121960 Sep 03 '18 at 13:32

2 Answers2

2

A value part of an object literal, such as in a name: function(){return;} property, is an expression.

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
  • Based on my experience on SO, I can blindly accept your word, but for others, please add some reference if possible. – Rajesh Sep 03 '18 at 13:26
  • A reference link for readers: [Expression - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Expressions) – Rajesh Sep 03 '18 at 13:28
  • @Rajesh I don't think linking the spec would help, and I don't know a good resource that explains how the expression grammar works – Bergi Sep 03 '18 at 13:32
  • The whole idea with request of reference was that currently the answer looks similar to a comment. Though its accurate, adding some explanation what defines an expression would make it concrete. Hence I added a link to doc as it clearly says, *An expression is any valid unit of code that resolves to a value.* Since, you are assigning a value to a property of an object, it resolves to a value and hence its an expression. But this maybe opinionated. So if you find my comment not relevant, we can skip it. :-) – Rajesh Sep 04 '18 at 06:43
1

An expression is any valid set of literals, variables, operators and expressions that evaluates to a single value. We distinguish expressions to

  • Assignment Expressions - x = 7
  • Evaluation expressions - 3 + 4 - commonly written in curly brackets (x + y)

While what our goal behind these two expressions is different, it doesn't matter if they consist of 3 characters or a code that goes to 40 lines*, in the end, they all evaluate to a single decisive value, be it certain primitive value (number, string, boolean, null, undefined, symbol) or an object


Meanwhile, a declaration is well.. precisely as a name suggests.. a declaration of something

var x;

declares an undefined variable of name x

functions in are simply sub-types of the object data type. So by doing function foo (){} We simply declare an object (function) that to the top of the module.


Keep in mind, declaration and expressions do not have to be some sort of separate techniques that may never be combined, in fact, in real JS world it's quite the opposite:

var obj = {
  foo: function bar() { console.log('baz'); }
};

Contains declaration of object obj. Assignment expression and declaration of function bar to the propety foo and also expression of the function bar itself.

Perhaps a much easier way to comprehend this would be:

var x = 7;

Where we first declare a variable x (var x) which is hoisted by the compiler to the top of our scope and then we assign it x = 7; but as a line by itself, it contains both a declaration and an expression

Samuel Hulla
  • 5,279
  • 6
  • 25
  • 51
  • Thank you for the detailed explanation, my specific problem is deeper than this and maybe I should have outright asked it. I have a couple of data sets that I am pulling using curried functions. The Base intermediate functions created with the curry are the same. So function = functionName(true)(true) – jk121960 Sep 03 '18 at 13:45
  • The first one works as it should but the second has mangled data. So it is a curry3, the first two parameters are common the third is the key or filter needed for the data query. the second one by itself will run fine also, just when I run both is there a problem. The form of the curry is basic function name(func) {return function(value1) {return function(value2, etc ending in a call to the initial "func" and passing in of values. And ideas would be great I can't figure it out. thanks again for your time. – jk121960 Sep 03 '18 at 13:45
  • @jk121960 honestly, it's probably better to ask that as a separate question. That's too much of a detail for me to give you an accurate answer via comment and I'd probably need to see the entire code to give you a definititve one at that. It also depends how your curried functions are evaluated, if they have any kind of closure, asynchronicity and what not – Samuel Hulla Sep 03 '18 at 13:49
  • function curry3( func ) { return function( first ) { return function( second ) { return function( third ) { return function( fourth ) { return func( first, second, third, fourth ); }; }; }; }; }; – jk121960 Sep 03 '18 at 13:49
  • "*We distinguish expressions to Assignment Expressions and Evaluation expressions*" - do we? On what basis? – Bergi Sep 03 '18 at 14:02
  • "*declaration of property `foo`*" - Properties are no declarations. They're parts of object literals, nothing else. – Bergi Sep 03 '18 at 14:03
  • @Bergi corrected the part about property. You are indeed right on that one. As to the other topics, perhaps *dinstguish* isnt best of words to use, because well, there really is not official dinguishment to be made in `expressions`, but in terms of the question, I feel like it helps to understand what all can be an expression – Samuel Hulla Sep 03 '18 at 14:22