Home dynamic object definition
Reply: 3

dynamic object definition

Kacper Cieluch
1#
Kacper Cieluch Published in 2018-02-12 09:40:15Z

This is a serious question, It has been nagging me for a while. In JavaScript you can declare a variable which has no type. It's type is often dynamic, depends on further value assigned to it.

e.g

var text;
typeof(text); //undefined

text = 'someText';
typeof(text); //string

So as we know that Javascript can dynamically change variable's type why these assignments are invalid then?

e.g

var someObj;
someObj.a = 'hello world';
console.log(someObj) //TypeError


var someTable;
someTable[0] = 'hello world';
console.log(someTable[0]) //TypeError

where this problem can be fixed just by adding definition to variable declaration:

var someObj = {};
var someTable = [];

I'd expect a good explanation of this problem. Thanks for answers.

MLavoie
2#
MLavoie Reply to 2018-02-12 10:21:54Z

1) Data types in Javascript are not dynamic, they are mutable. This means that depending on kind of operation you are applying to them they can change from being something into another thing (e.g. a number can become a string doing something like this: 4 + 'a').

2) The "error" is a consequence of having a variable not initialized. In Javascript when you are doing this:var something = 'a' you are doing two operations, a declaration, and an assignment. If you don't do the assignment and you try to access to a property of the object you have declared it will throw an error. In your case, you are declaring a variable but not initializing it to the "compiler" can't access the "a" property or the position 1 because it is of type undefined.

James Monger
3#
James Monger Reply to 2018-02-12 11:15:03Z

You're getting confused about where the error is thrown. In your first example:

var someObj;
someObj.a = 'hello world'; // the error is thrown here

That error is thrown because someObj is undefined (not undeclared). So, you can't access the property a on undefined.

The same explanation applies for your second example:

var someTable;
someTable[0] = 'hello world'; // the error is thrown here

You're trying to access index 0 of undefined.

The reason that defining them as {} and [], respectively, fixes your issue, is that you are then accessing property a of {} and index 0 of [].

chŝdk
4#
chŝdk Reply to 2018-02-12 09:57:10Z

There's a big difference between declaration and initialisation of variables.

When you declare a variable without initializing it with a value, its type will be undefined, so when you will try to call it or access it, it will give undefined, because simply there were no value defined for the variable.

That's why it should be initialized:

var someObj = {};
var someTable = [];

So you can treat it as a string, an object or an array according its initialized value.

Documentation:

Please take a look at variables MDN Reference for further reading, where it says:

A var statement declares variables that are scoped to the running execution context’s VariableEnvironment. Var variables are created when their containing Lexical Environment is instantiated and are initialized to undefined when created. [...] A variable defined by a VariableDeclaration with an Initializer is assigned the value of its Initializer’s AssignmentExpression when the VariableDeclaration is executed, not when the variable is created.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.31815 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO