3

From what i can infer from these 2 sources:

Source 1 and Source 2

is that in javascript when a primitive datatype is used in the context of the object, it is internally converted to an object. But this is just a temporary conversion in that the object will be removed from memory after it is used.

My question is what is the advantage of using such an approach? Is it to just save memory? or is there any other advt.?

Community
  • 1
  • 1
depz123
  • 497
  • 5
  • 18
  • It means you don't need to specify a type for each variable; `var string = "somestring"; string = 1234; // no error thrown` – Paul S. Jan 27 '13 at 14:50
  • 1
    "*The advantage of using such an approach*" …over what? – Bergi Jan 27 '13 at 17:00
  • var str = 'hello'; var length = str.length; In above statement, the str will be internally converted to an object and deleted after the statement is executed. But in a php for instance we have a simple function to do the same thing instead of creating an object. what is the advt of using the javascript approach? – depz123 Jan 27 '13 at 17:07

2 Answers2

1

It's just because JavaScript is a prototype-based program language.

Prototype-based programming is seen as encouraging the programmer to focus on the behavior of some set of examples and only later worry about classifying these objects into archetypal objects that are later used in a fashion similar to classes. As such, many prototype-based systems encourage the alteration of prototypes during run-time.

That determines you can do something like this:

var str = 'foo'
str.weight // just return 'undefined'

var yes = true, no = false
yes.__proto__.length = function() { return this.toString().length; }
yes.length() // 4
no.length() // 5

var num = 10, amount = 0
num.__proto__.plus = function(num) { return this + num; }
amount.plus(num) // 10

But unfortunately, some features of JavaScript were influenced by Java, for example, the primitive vs. object distinction, so you get these wired things:

var str = 'foo'
str.weight = 42
str.weight // undefined

str = new String('foo')
str.weight = 42
str.weight // 42

1.toString() // error!
var n = 1
n.toString() // '1'
Chris
  • 1,264
  • 1
  • 10
  • 14
1

I believe that one advantage JavaScript offers because of what you mention is easy type coercion.

As one comment implied earlier, JavaScript is loosely typed; you can declare a variable without having to declare what kind of variable it is and therefore without knowing what you're doing with it. This has simple advantages like being able to write:

var str = 'hello user #',
    num = 3,
    sayhello = str + num;

alert(sayhello); // alerts 'hello user #3'

Notice here how the number can simply be added to the string as if itself a string.

Therefore many operators and methods are available that perhaps wouldn't be so easy to use in a more strongly typed language. You can use the parseInt method on an argument without having to check or convert the type of the argument first:

var returnWholeNumber = function (arg) {
        return parseInt(arg, 10);
    },
    str = '5 is the answer',
    num = 8.19437;

alert(returnWholeNumber(str)); // alerts number 5
alert(returnWholeNumber(num)); // alerts number 8

By supplying a temporary object wrapper, JavaScript saves you from having to do some conversions yourself. It simply supplies the wrapper based on what you trying to do and then discards it. As a result, JavaScript can be much more dynamic and expressive than more strongly typed languages.

This is also useful for conditionals. Some values (e.g., 0 or an empty string '') are what's known as falsey. That way you can do a simple boolean check on them and JavaScript will wrap the primitive data type in a Boolean wrapper.

if (!userInput) {
    alert('no input');
}

However, type coercion can be confusing and requires caution:

alert('1' + 2 + 3); // alerts 123
alert('1' + '2' + '3'); // alerts 123
alert(1 + 2 + 3); // alerts 6

Also for conditionals. Use triple equals when checking type, to avoid unintended coercion:

var i = '3';
if (i == 3) { // type-coercing conditional; i can be a string or a number and this will be true
    alert('i is 3'); // will successfully alert
}
if (i === 3) { // type-checking conditional; i has to be an actual number (and the value must be 3)
    alert('i is a number 3'); // will not alert because i is a string
}
guypursey
  • 3,114
  • 3
  • 24
  • 42