I'm messing about with a javascript pattern that will allow me to namespace my code and to define shortcuts internally out of the global scope to reduce the amount of typing I will have to do.
Things like $ instead of jQuer开发者_StackOverflow中文版y or $messageType instead of messages.messageType.
Whilst the pattern seems to be working nicely I have lost certain intellisense functionality now in visual studio 2010.
e.g. My test function below will alert "success" but my intellisense doesn't enumerate through the $messageType object properties.
Since productivity is the key this is a big issue for me.
Is there something i've missed that the javascript gurus can pick up on?
Here's a jsfiddle to play about with.
; (function (window) {
// Define a local copy of myObject
var myObject = function () {
// The myObject object is actually just the init constructor 'enhanced'
return new myObject.fn.init();
},
// Shortcuts.
// A central reference to the root messages object
$messages = null,
// A central reference to the root messages.messageType object
$messageType = null;
myObject.fn = myObject.prototype = {
init: function () {
// Initialise the object shortcuts.
$messages = this.messages;
$messageType = this.messages.messageType;
}
};
// Give the init function the myObject prototype for later instantiation
myObject.fn.init.prototype = myObject.fn;
myObject.fn.messages = {
/// <summary>
/// Provides means to provide feedback message to the client.
/// </summary>
messageType: {
information: "information",
error: "error",
success: "success"
}
};
myObject.fn.tester = function () {
alert($messageType.success);
};
// Expose myObject to the global object
window.myObject = window.$m = myObject();
} (window));
jQuery(document).ready(function () {
$m.tester();
});
Doh.... I forgot to return the object in my init function!!
myObject.fn = myObject.prototype = {
init: function () {
// Initialise the object shortcuts.
$messages = this.messages;
$messageType = this.messages.messageType;
// It took jslint ans a cup of coffee to figure this out :)
return this;
}
};
精彩评论