In this example notice has a property called message:
function notice(action) {
notice.message = notice.message + " feels like " + action;
}
notice.message = "coding";
notice("fighting");
alert("if " + notice.message + " you are in the wrong profession");
When I saw this talk by Kyle Simpson he was giving out copies of books he authored, which was a surprise, and I came away from the talk with a copy of "this & OBJECT PROTOTYPES" which one of many books in his greater "You Don't Know JS" series. (JS being JavaScript, not jack shit.) I see an opportunity to comb through this book and put together little coding examples for myself not unlike I did for C# 4.0 in a Nutshell by Joseph and Ben Albahari.
The trick here comes up in the first chapter which tries to explain the this keyword and also talks about how you may do what I do above with lexical scoping. Inside the function you have to have notice.message and not just message or certainly not this.message as the later two will not fly. This way of the function explicitly referring to itself is kinda gross and counterintuitive and hence I have never thought to do this before. I like that message can be defined after the fact (i.e. after the function itself is defined) as anything can be monkey-patched in JavaScript. Er, maybe monkey-patched is the wrong term. That might refer to overpowering (replacing) one setting with another sidestepping any type safety or comparable restraints. We are instead bolting on a property, right? Oh, it's all so confusing. ;)
No comments:
Post a Comment