javascript设计模式不返回某些对象的智能感知

发布于 2024-10-25 09:16:13 字数 1752 浏览 1 评论 0原文

我正在研究一种 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄟ。诗瑗 2024-11-01 09:16:13

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;
    }
};

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;
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文