Object.create(new.brain)#205

Describe one thing you’re learning in class today. Why do you think it will be important in your future web development journey?

Today in class we learned out Object.create( ). It’s a static method on the object prototype that creates a new object with the prototype set to a certain object. This component to Javascript is a great way to use a method to an object, but not have that method applied to every single other object in an app or project you are working on.

Create JavaScript Object with create method

Object.create() allowed to create object with more attribute options like configurable, enumerable, writable and value as shown below

prototype

  • Every single object is built by constructor function.
  • A constructor function makes an object linked to its own prototype.
  • Prototype is an arbitrary linkage between the constructor function and object.

Can you offer a use case for the new arrow => function syntax?

An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords. Arrow function expressions are all suited as methods, and they cannot be used as constructors.

const materials = [‘hydrogen’,‘helium’,‘lithium’,beryllium’];

console.log(materials.map(material => material.length));

output: Array [8,6,7,9]

How does this new syntax differ from the older function signature, function nameFunc(){}, both in style and functionality?

Arrow functions, sometimes called ‘fat arrow’ functions, utilizing the new token =>. These functions two major benefits — a very clean concise syntax and more intuitive scoping and this binding.

Explain the differences on the usage of foo between function foo() {} and const foo = function() {}

One is a function declaration (function foo() {}), the other is a function expression (const foo = function() {}:). Function declarations are evaluated upon entry into the enclosing scope, before any step-by-step code is executed. Function expressions are evaluated as part of the step-by-step code, at the point where they appear (just like any other expression). That one creates a function with no name, which it assigns to the foo variable. — They are evaluated at different times, have different effects on the scope in which they’re defined, and are legal in different places.

What advantage is there for using the arrow syntax for a method in a constructor?

Gives you access to “this”, without having to bind to the class.

Can you give an example for destructuring an object or an array?

It’s called “destructuring assignment,” because it “destructurizes” by copying items into variables. But the array itself is not modified.

const foo = [‘one’, ‘two’, ‘three’];

const [reptile, amphibian, mammal]= foo

console.log(reptile) // “one”

console.log(amphibian) // “two”

console.log(mammal)// “three”

Explain Closure in your own words. How do you think you can use it? Don’t forget to read more blogs and videos about this subject.

“Closures” are important because they control what is and isn’t in scope in a particular function, along with which variables are shared between sibling functions in the same containing scope. closures are created every time a function is created, at function creation time.

To “use a closure”, define a function inside another function and expose it. To expose a function, return it or pass it to another function. The inner function will have access to the variables in the outer function scope, even after the outer function has returned.

Closures are frequently used in JavaScript for object date privacy, in event handlers and callback functions, and in partial applications, currying, and other functional programming patterns.

Austin Coding Academy