Java Reference
In-Depth Information
function party() {
console.log("Wow this is amazing!");
party = function(){
console.log("Been there, got the T-Shirt");
}
}
party.music = "Classical Jazz"; // set a
property of the
function
party();
<< "Wow this is amazing!"
party.music; // function has now been redefined,
so the
property doesn't exist
<< undefined
Init-Time Branching
This technique can be used with the feature detection that we discussed in the last chapter to
create functions that rewrite themselves, known as init-time branching . This enables the
functions to work more effectively in the browser, and avoid checking for features every
time they're invoked.
Let's take the example of our fictional unicorn method that's yet to have full support in
all browsers. In the last chapter, we looked at how we can use feature detection to check if
this object is supported. Now we can go one step further: instead of just running the relev-
ant code, we can rewrite the function so that it only runs the relevant code without the need
to check for support every time it's called:
function ride(){
if (window.unicorn) {
ridePony = function(){
return "Riding on a unicorn is great fun!";
Search WWH ::




Custom Search