Javascript reference datatype

From what I can infer from these two sources:

Source 1 and Source 2

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

My question is, what is the advantage of using this approach? Isn't it easy to keep memory? or is there another one?

+3


source to share


2 answers


This is simply because JavaScript is a prototype-based program language.

Prototype-based programming is seen as encouraging the programmer to focus on the behavior of certain sets of examples and only later worry about classifying these objects into archetypal objects that are later used in a class-like fashion. Thus, many prototype-based systems facilitate changes to prototypes at runtime.

This determines that 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 JavaScript features have been influenced by Java, such as primitive versus object differences , 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'

      

+1


source


I believe one advantage of JavaScript in what you say is simple type coercion.

As one comment implied earlier, JavaScript is freely typed; you can declare a variable without specifying what variable it is and therefore not knowing what you are doing with it. This has simple advantages such as being able to write:

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

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

      

Notice how the number can be simply added to the string, as if it were the string itself.

Therefore, there are many operators and methods available that might not be so easy to use in a more strongly typed language. You can use a method parseInt

on an argument without having to first check or convert the type of the argument:

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 providing a temporary wrapper for the object, JavaScript saves you the trouble of doing some of the transformations yourself. It just supplies a wrapper based on what you are trying to do and then discards. As a result, JavaScript can be much more dynamic and expressive than more strongly typed languages.



This is also useful for legend. Some values ​​(for example, 0

or an empty string ''

) are called false. This 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, the type of 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 legend. Use a ternary equivalent when type checking to avoid unintentional 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
}

      

+1


source







All Articles