Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorthand to declaring empty object properties in Javascript, is there any?

I need to declare a lot of object properties in my script and I wonder if the're any way to shorten this:

Core.registry.taskItemSelected;
Core.registry.taskItemSelected.id;
Core.registry.taskItemSelected.name;
Core.registry.taskItemSelected.parent;
Core.registry.taskItemSelected.summary;
Core.registry.taskItemSelected.description;
like image 935
Edward Avatar asked May 19 '11 10:05

Edward


People also ask

What is property shorthand in JavaScript?

Object property shorthand enables us to simply pass in the name of the key as opposed to repeating the name and the key.

How do I create an empty object in JavaScript?

Use a for..in loop to clear an object and delete all its properties. The loop will iterate over all the enumerable properties in the object. On each iteration, use the delete operator to delete the current property.

How check if object is empty JavaScript?

Use Object. Object. keys will return an array, which contains the property names of the object. If the length of the array is 0 , then we know that the object is empty.

What does an empty object return in JavaScript?

Checking empty object with JavaScript It will return false and not throw a TypeError .


1 Answers

Wouldn't that work?

Core.registry.taskItemSelected = {
  id: null,
  name: null,
  parent: null,
  ...
};
like image 195
Félix Saparelli Avatar answered Sep 30 '22 17:09

Félix Saparelli