Exercises

Lets solve the following exercise:

  1. Look at the following code:
            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?

  2. What's the result of executing this piece of code?
            function C(){  
              this.a = 1; 
              return false; 
            } 
            console.log(typeof new C()); 
    
  3. What's the result of executing the following piece of code?
            > c = [1, 2, [1, 2]]; 
            > c.sort(); 
            > c.join('--'),  
            > console.log(c);  
    
  4. Imagine the 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"] 
    

    Note

    You can use a for loop to loop through the input string, treating it as an array.

  5. Update your MyString() constructor to include a reverse() method.

    Note

    Try to leverage the fact that arrays have a reverse() method.

  6. Imagine that 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" 
    
    • If you found this exercise amusing, don't stop with the join() method; go on with as many methods as possible.
  7. Imagine 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 array
    • MyMath.max(array): This returns the largest number in a given array
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.131.178