Wrapper Objects Back
Overview
- we can use properties to an object, a value or a method
var s = "Aloha";
s.substring(s.indexOf("A"), s.length);
- there are no wrapper objects for the null and undefined values, which will cause a TypeError when attempt to access.
- strings, numbers and boolean values behave like objects when you try to read the value of a property from them. But if you attempt to set the value of a property, that attempt is silently ignored.
var s = "test";
s.len = 4;
var t = s.len;
- all string methods that appear to return a modified string are, infact, returning a new string value, cause string is immutable.
var s = "Aloha"
s.toUpperCase();
s
var o = {x: 1};
o.x = 2;
o.y = 3;
var a = [1, 2, 3];
a[0] = 0;
a[3] = 4;
Compare
var a = 3;
var b = "3";
a == b
a === b
- distinct Array and Object are never equal, but equal when two reference points to the same thing.
var a = [], b = [];
a == b
a === b
var a = {x: 1}, b = {x: 1};
a == b
a === b
var a = [];
var b = a;
a == b
a === b
- when wanting to compare two arrays by checking their values, we can write a function.
function equalArray(a, b){
if(a.length != b.length)
return false;
for(var i = 0; i < a.length; i++)
if(a[i] !== b[i])
return false;
return true;
}