簡體   English   中英

Javascript:如何聲明非全局靜態方法?

[英]Javascript: How can I declare non-global static methods?

如何在js中聲明非全局靜態方法?

foo.bar = function() {
  function testing{
    console.log("statckoverlow rocks!");
  }
  function testing2{
    console.log("testing2 funtction");
  }
}

如何調用測試功能? 我是JS的新手。

感謝幫助。

您可能想要一個對象。

foo.bar = {
  testing: function() {
    console.log("statckoverlow rocks!");
  },
  testing2: function() {
    console.log("testing2 funtction");
  }
};

然后,例如,調用foo.bar.testing()

您可以這樣:

foo.bar = (function() {
  var testing = function () {
    console.log("statckoverlow rocks!");
  };
  var testing2 = function () {
    console.log("testing2 funtction");
  };
  return {
    testing: testing,
    testing2: testing2
  };
}());

// call them
foo.bar.testing();
foo.bar.testing2();

你的意思是:

var foo = {
    bar: {
        testing: function()
        {
            console.log("statckoverlow rocks!");
        },
        testing2: function()
        {
            console.log("testing2 funtction");
        }
    }
};


foo.bar.testing();
foo.bar.testing2();
// Constructor
function Foo() {
  var myvar = 'hey'; // private
  this.property = myvar;
  this.method = function() { ... };
}
Foo.prototype = {
  staticMethod: function() {
    console.log( this.property );
  }
}
var foo = new Foo();
foo.staticMethod(); //=> hey

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM