Lets solve the following exercise:
function F() { function C() { return this; } return C(); } var o = new F();
Does the value of this
refer to the global object or the object o
?
function C(){ this.a = 1; return false; } console.log(typeof new C());
> c = [1, 2, [1, 2]]; > c.sort(); > c.join('--'), > console.log(c);
String()
constructor didn't exist. Create a constructor function, MyString()
, that acts like String()
as closely as possible. You're not allowed to use any built-in string methods or properties, and remember that the String()
doesn't exist. You can use this code to test your constructor:> var s = new MyString('hello'), > s.length; 5 > s[0]; "h" > s.toString(); "hello" > s.valueOf(); "hello" > s.charAt(1); "e" > s.charAt('2'), "l" > s.charAt('e'), "h" > s.concat(' world!'), "hello world!" > s.slice(1, 3); "el" > s.slice(0, -1); "hell" > s.split('e'), ["h", "llo"] > s.split('l'), ["he", "", "o"]
MyString()
constructor to include a reverse()
method.Array()
and the array literal notation don't exist. Create a constructor called MyArray()
that behaves as close to Array()
as possible. Test it with the following code:> var a = new MyArray(1, 2, 3, "test"); > a.toString(); "1,2,3,test" > a.length; 4 > a[a.length - 1]; "test" > a.push('boo'), 5 > a.toString(); "1,2,3,test,boo" > a.pop(); "boo" > a.toString(); "1,2,3,test" > a.join(','), "1,2,3,test" > a.join(' isn't '), "1 isn't 2 isn't 3 isn't test"
join()
method; go on with as many methods as possible.Math
didn't exist. Create a MyMath
object that also provides the following additional methods:MyMath.rand(min, max, inclusive)
: This generates a random number between min
and max
, inclusive if inclusive
is true
(default)MyMath.min(array)
: This returns the smallest number in a given arrayMyMath.max(array)
: This returns the largest number in a given array18.119.131.178