http://webreference.com
As explained in the previous page, JavaScript 1.3 includes two new methods for the
exterior.call(this, extColor, doorCount, airWing, tireWidth);
The
exterior.apply(this, arguments);
What it means is that all of the caller's parameters are passed on to the callee. In the automobile assembly line from the previous page, the parameters of the caller (
intColor, seatCoverType, benchOption, extColor, doorCount, airWing, tireWidth
These parameters are passed to the callee which is the
function exterior(intColor, seatCoverType, benchOption, extColor, doorCount, airWing, tireWidth ) { this.extColor = extColor; this.doorCount = doorCount; this.airWing = airWing; if (tireWidth > 10) this.wideTire = true; else this.wideTire = false; }
The whole script is very similar to the one presented in the previous page, except the
Now, it is very easy to add a new station to our Volvo assembly line. Let's assume a new door station has been added, and the
function doors(intColor, seatCoverType, benchOption, extColor, doorCount, airWing, tireWidth ) { this.doorCount = doorCount; }
Of course, we have to remove this assignment from the
function automobile(intColor, seatCoverType, benchOption, extColor, doorCount, airWing, tireWidth ) { interior.apply(this, arguments); exterior.apply(this, arguments); doors.apply(this, arguments); }
The whole script will look like this now:
The object-oriented structure of the script makes it easier to add more stations. Also notice that adding an automobile feature requires the extension of all parameter lists by one. |
版权声明:本文为博主原创文章,未经博主允许不得转载。