Modern JavaScript

Best of Modern JavaScript — Prototypes and Function Names

Spread the love

Since 2015, JavaScript has improved immensely.

It’s much more pleasant to use it now than ever.

In this article, we’ll look at properties in JavaScript.

Using call and apply to Call hasOwnProperty() Safely

hasOwnProperty is a method of an object’s prototype.

Therefore, it can easily be overridden with an object’s own methods.

To call hasOwnProperty safety, we can call it with call .

So instead of writing:


We write:, 'prop')

The 2nd way is safer because hasOwnProperty is always part of the Object.prototype .

We can’t guarantee that hasOwnProperty isn’t overridden with the first way.

With ES6, we can use the Map constructor to store key-value pairs, so that we don’t need to create objects to store them.

Therefore, we won’t need hasOwnProperty as much.

Abbreviations for Object.prototype and Array.prototype

Using Object.prototype and Array.prototype are long.

But we can shorten Object.prototype to an empty object literal.

We can shorten Array.prototype to an empty array literal.

So instead of writing:, 'prop')

We can write:

({}), 'prop')

With arrays, we can write:


The name Property of Functions

The name property of the function contains the function’s name.

For example, we can write:

function foo() {}

Then returns 'foo' .

Arrow functions also has the name property.

For instance, if we write:

`const` bar `=` `()` `=>` `{};`

Then returns 'bar' .

Default Values

If we use a function as a default value, then it gets its name from its variable or parameter.

For example, if we have:

`let` `[foo =` `function` `()` `{}]` `=` `[]`

Then is 'foo' .


`let` `{` bar`:` foo `=` `function` `()` `{}` `}` `=` `{};`


`function` `g(foo =` `function` `()` `{})` `{`
  `return` `;`

all get the same result.

Named Function Definitions

If we have function declarations, then the name property of the function will have the name:

function foo() {}
console.log(; would be 'foo' .

For function expressions, we get the same thing:

const bar = function baz() {};

so is 'bar' .

However, if we assigned a named function to a variable, then the function’s name would be the function’s name.

For example, if we write:

const bar = function baz() {


Then we call it with bar and would be baz .

But we can’t write baz() to call it, we’ll see the ‘Uncaught ReferenceError: baz is not defined’ error.

Methods in Object Literals

Methods in object literals can be defined with fixed and computed property names.

For instance, we can write:

function qux() {}

let obj = {
  foo() {},
  bar: function() {},
  ['ba' + 'z']: function() {},

foo is defined with the object method shorthand.

bar is defined as a traditional method.

baz is defined with the computed key.

And qux is passed in from the outside.

If we get the name property of each method:


We get:



We can use the name property to get the property name of a function.

Also, we can call a constructor instance’s method in shorter ways in some situations.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.