javascript设计模式不返回某些对象的智能感知
我正在研究一种 javascript 模式,该模式允许我为代码命名并在全局范围之外内部定义快捷方式,以减少我必须执行的输入量。
像 $ 而不是 jQuery 或 $messageType 而不是 messages.messageType。
虽然该模式似乎运行良好,但我现在在 Visual Studio 2010 中失去了某些智能感知功能。
例如,下面的测试函数将警告“成功”,但我的智能感知不会通过 $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();
});
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 jQuery 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();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Doh....我忘了在我的 init 函数中返回该对象!
Doh.... I forgot to return the object in my init function!!