1

嗨,我希望有人可以在这里帮助我:) 我正在尝试设置一个类似于 jQuery 的时尚库。这是我到目前为止所拥有的。

/*!
 * Sentinal Javascript Library V1.0.0
 * http://sentinal.com
 *
 * Depends on jQuery v1.9.1
 *
 * Copyright 2013 Sentinal Team and other contributors
 * Released under license
 * http://sentinal.com/license
 *
 * Date 25-3-2013
 */ 
(function ( window, location, $, undefined ){

// Can't do this because several apps including ASP.NET trace
// the stack via arguments.caller.callee and Firefox dies if
// you try to trace through "use strict" call chains. (#13335)
// Support: Firefox 18+
//"use strict";
var 
    // A central reference to the root Sentinal(document)
    rootSentinal,

    // Use the correct document accordingly with window argument (sandbox)
    document = window.document,
    location = window.location,

    // Map over Sentinal in case of overwrite   
    _Sentinal = window.Sentinal,

    // Map over the $S alias in case of overwrite (Namespace alias for Sentinal)
    _$S = window.$S,

    // Version of this library 
    core_version = "1.0.0",

    // Define a local copy of Sentinal
    Sentinal = function() {
        // The Sentinal object is actually just the init constructor 'enhanced'
        return new Sentinal.fn.init();
    };

    Sentinal.fn = Sentinal.prototype = {
        // The current version of Sentinal being used
        Sentinal: core_version,

        constructor: Sentinal,

        init: function (){
                $(document).ready(function(){
                $('body').append('<h1>It Works!</h1>');
                console.log( "Sentinal Version is reporting : " + $S.fn.version() );
            });
        },

        version: function (){
            return core_version;
        },

        render: function (){

        },

        send: function () {

        },

        recieve: function () {

        }

    };



    // Give the init function the jQuery prototype for later instantiation
    Sentinal.fn.init.prototype = Sentinal.fn;

    window.Sentinal = window.$S = Sentinal;

})( window, location, jQuery );

我现在遇到的问题要么与我的图书馆不知道 $(document).ready 有关。或者是范围问题,但是其中包含一些 jQuery 的 init 方法不会将 html 附加到文档的正文中,并且 console.log 也不起作用。

任何信息都将受到极大的欢迎:)

4

1 回答 1

0

匿名函数并不总是自行执行。它们由传递给它们的函数(或通过参数对象访问)命名。

您的问题是您永远不会调用 init 函数,这意味着永远不会调用 $(document).ready() 函数,因此您的匿名函数永远不会附加到文档的 ready 事件。

尝试完全删除 init 函数,并在代码末尾调用 $(document).ready() ,如下所示:

. . .  

window.Sentinal = window.$S = Sentinal;

$(document).ready(function(){
            $('body').append('<h1>It Works!</h1>');
            console.log( "Sentinal Version is reporting : " + $S.fn.version() );
        });

// Give the init function the jQuery prototype for later instantiation
Sentinal.fn.init.prototype = Sentinal.fn;

})( window, location, jQuery );

或者,您可以像这样立即调用您的 init 函数:

        init: function (){
            $(document).ready(function(){
            $('body').append('<h1>It Works!</h1>');
            console.log( "Sentinal Version is reporting : " + $S.fn.version() );
        });
    }(); // call the function immediately,

此外,您不需要将 window 对象传递到您的插件中,因为它是全局对象,因此可以随时随地访问。

于 2013-03-25T16:50:40.747 回答