繁体   English   中英

无法调用Javascript对象方法

[英]Can't call Javascript object method

我得到一个控制台错误TypeError: bodys.show is not a function在加载我的页面时TypeError: bodys.show is not a function 我究竟做错了什么?

function loadingScreen(selector,css)
{
    this.loadingAnimation = $("<div><p>Loading</p><div></div></div>").appendTo(selector);
    this.containerCss = $.extend({'border-radius':"20px",position:"absolute",height:"40px",width:"120px","display":"none"}, css);
    this.loadingAnimation.css(this.containerCss);
    this.p = this.loadingAnimation.children("p").first();
    this.p.css({width:"100%",'text-align':"center"});
    this.div = this.loadingAnimation.children("div").first();
    this.div.css({position:"abslolute",left:"0",top:"0",'background-color':"rgba(255,100,100,0.5)",height:"100%",width:"10%"});
    function show(){
        this.loadingAnimation.css("display","block");
        animate(this.div,"right");
    }
    function hide(){
        this.loadingAnimation.css("display","none");
        this.div.stop(true,true);
        this.div.css("margin-left","0px");
    }
    function animate(element,direction)
    {
        if(direction === "right"){
            element.animate({"margin-left":"120px"},animate(element,"left"));
        }
        else if(direction === "left"){
            element.animate({"margin-left":"0px"},animate(element,"right"));
        }
    }
}

$(document).ready(function()
{
    var bodys = new loadingScreen("body",{});
    bodys.show();
});

在函数体内声明变量/函数时,它们是“私有的”,除非它们作为函数本身的属性附加 - 在体内,您将使用this关键字来执行此操作。

该功能show()是私有的内部loadingScreen,使其作为其父的成员函数访问,与声明它this

this.show  = function(){...

...}

...它仍然可以访问loadingScreen范围内的loadingScreen ,但是可以作为loadingScreen的方法在外部loadingScreen

编辑:

正如Naomik在下面指出的那样,你也可以将它附加到对象的原型:

loadingScreen.prototype.show = function()...

...显然原型函数比标准成员声明执行得更快,但是 - 因为你将在主函数体之外声明它,它将无法访问函数内部的私有变量,所以在这种情况下它没有多大用处。

目前show()函数是在loadingScreen的本地/私有范围内loadingScreen ,为了使show公开可见使用this.show

所以改变你的代码

 this.show = function() {

代替

function show(){

您可以使用this.show = show;show到您的新实例this.show = show; 在你的构造函数中

或者你可以做到这一点

// loading-screen.js
(function(window, $) {

  var LoadingScreen = function (selector, css) {
    this.$elem = $(selector);
    this.$elem.css(css);
  };

  LoadingScreen.prototype.show = function () {
    return this.$elem.show();
  };

  LoadingScreen.prototype.hide = function () {
    return this.$elem.hide();
  };

  window.LoadingScreen = LoadingScreen;

})(window, jQuery);

作为原型的一部分,现在可以访问任何LoadingScreen实例的showhide函数。 最重要的是,你有一个很好的小代码模块,可以与所有其他脚本分开包含。

用法

<script src="loading-screen.js"></script>
<script>
  $(function() {
    var ls = new LoadingScreen("body", {});
    ls.show();
    ls.hide();
  });
</script>

jQuery插件 - 一个演示

既然你正在使用jQuery,而不是将selector和一些css传递给vanilla构造函数,那么将它包装在jQuery插件中可能是有意义的。

(function(window, $) {

  var LoadingScreen = function($elems, css) {
    this.$elems = $elems;
    this.$elems.css(css);
  };

  LoadingScreen.prototype.show = function() {
    this.$elems.slideDown(250);
  };

  LoadingScreen.prototype.hide = function() {
    this.$elems.slideUp(250);
  };

  $.fn.loadingScreen = function(css) {
    return new LoadingScreen(this, css);
  };

})(window, jQuery);

用法非常相似,但看起来更传统的jQuery

$(function() {
  var ls = $("p").loadingScreen({backgroundColor: "blue"});

  // notice .show and .hide delegate to our plugin methods now
  ls.show();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM