我正在搞乱一个javascript模式,它允许我命名我的代码并在全局范围内定义快捷方式,以减少我必须要做的输入量。
像$而不是jQuery或$ messageType而不是messages.messageType。
虽然这种模式看起来效果很好但我现在已经在visual studio 2010中丢失了某些智能感知功能。
e.g。我下面的测试函数会提示“成功”但我的intellisense不会通过$ messageType对象属性进行枚举。
由于生产力是关键,这对我来说是个大问题。
有没有我错过了javascript专家可以接受的东西?
这是一个jsfiddle来玩。
; (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();
});
答案 0 :(得分:0)
Doh ....我忘了在init函数中返回对象!!
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;
}
};