Page 162 - DCAP202_Fundamentals of Web Programming
P. 162

Unit 13: Building Object of JavaScript




          As you can see from the code none of the values returned from any of out three methods are  Notes
          actually being used since none of them have the returned value assigned to anything. Quite
          probably the way you currently have these methods defined you don’t have them returning
          anything at all. We are going to change that.
          What we need to do with all these methods that do not currently return anything is to add one
          extra line of code to the end of each of the methods.
          return  this;
          With that line of code in place our methods now each return the object that they belong to This
          is the only change that we need to make to methods in order to allow us to use method chaining.
          Of course we can’t add that line if the method already returns a value but them methods that
          return values are not suited to method chaining in the first place as each of those needs to be
          specified separately in order to be able to assign the value that is returned.
          With our methods that don’t normally need to return a value now returning a reference to the
          object that they belong to it is no longer necessary for us to use three separate statements to call
          those three methods. We can now combine all three into one statement simply by chaining the
          methods together.
          var  newB  =  new  example();
          newB.firstMethod(‘x’).secondMethod(‘y’).thirdMethod(1,15,’z’);
          This works because the last statement in firstMethod() is return this; which in this case returns a
          reference to newB. This means that newB.firstMethod() after the method has run is equivalent to
          newB and so can be substituted for newB where we want the method to be run and the object to
          be available for further referencing.

          Self Assessment

          Fill in the blanks:

          5.   The .......................... object let’s you work with a series of characters and wraps Javascript’s
               string primitive data type with a number of helper methods.
          6.   The ............................ property allows you to add properties and methods to an object.


          13.3 The Math Object

          JavaScript provides all sorts of shortcut ways of specifying your code which makes the code
          quicker to write. Unlike the other global objects, Math is not a constructor. All properties and
          methods of Math are static and can be called by using Math as an object without creating it.
          Thus, you refer to the constant pi as Math.PI and you call the sine function as Math.sin(x), where
          x is the method’s argument.
                 Properties                           Description
           E                     The constant of E, the base of natural logarithms.
           LN2                   The natural logarithm of 2.
           LOG2E                  Base 2 logarithm of E.
           LOG10                 E Base 10 logarithm of E.
           SQRT1_2               Square root of 1/2.







                                           LOVELY PROFESSIONAL UNIVERSITY                                   155
   157   158   159   160   161   162   163   164   165   166   167