Echo JS 0.11.0

<~>
tracker1 1625 days ago. link 3 points
A lot of the details are based on implementations prior to ES6 in particular... and is inaccurate for any current implementation.

I tend to not like these 10-things in JavaScript type posts, as they're usually inaccurate or misleading... and in general remove or ignore them... here's some commentary and additional information regarding many of the questions specifically below.


1. What is closure in Javascript?

A closure in programming is the scope of a given variable.  Traditionally in JavaScript closures have been limited to global or in the scope of a function (also, see: hoisting).  As of ES6 const/let variables may be scoped to a more narrowly defined closure such as a loop or a defined curly-brace scoping.

For example, I now tend to manually create scopes in switch statement.

    switch (x) {
      case "somevalue": { // scope via curly braces
        const foo = x; // scoped variable
        ...
      }
      ...
    }

2. What is DOM in Javascript?

Document Object Model (DOM) is the Application Programming Interface used by JavaScript in the context of a web browser in an interactive page/tab/instance, not to be confused with WebWorkers which have a different browser context.

The rest is mostly accurate in the post.


3. What is Promise in Javascript?

A promise is an class/object that *should* resolve or reject with a callback interface for resolve/reject value.  It is used in conjunction with asynchronous code, but the API does not necessarily require it.  It's also referred to as a thennable in practice.


4. What is a prototype in Javascript?

Prototype is the method of enheritance in JavaScript.  It can be though of as a default search path.  Class functions, and classes may have a prototype based inheritance change, and when accessing a property/method attached to an instance, the instance then the prototype and it's prototype in tern will then be searched.

This is slightly different than how inheritance works in other languages.

TFA is incorrect regarding Object.prototype as objects may inherit from null via Object.create(null) as an inheritance end point.  This is to prevent accidental extension via Object where the behavior would be undesired.


5. What is hoisting in Javascript?

Mostly accurate.


6. What is an object in Javascript?

Other than some details regarding primitive values such as numbers, strings, null, undefined, and NaN; pretty much everything (including Arrays) in JavaScript is an object.  An object is any given value that contains properties and a prototype chain.

7. What is a function in Javascript?

There's also the Function() constructor.  Not to mention specifics around variations of functions in currently implementations including async, generator and fat-arrow functions (and context).


8. What is a pure function in Javascript?

Mostly correct.


9. What is a constructor in Javascript?

Constructor functions in JS are an implementation detail mostly about convention.  Constructor functions are meant to be called/initialized via the "new" keyword and will by convention use CamelCase naming.

Ironically, the example is actually NOT a constructor function, as it uses a fat-arrow function which will bind "this" to the higher level context.


10. What are Javascript classes?

the class construct in JavaScript is mostly syntax sugar offering a different syntax for creating constructor functions and related prototype details.  Instances are still prototype based.

Replies