【问题标题】:How to define public and private property in javascript如何在javascript中定义公共和私有属性
【发布时间】:2014-01-17 09:39:35
【问题描述】:

我想在 JavaScript 类中定义公有和私有属性,

在这里你可以看到我的属性的 c# 格式。

我的问题是“如何用 JavaScript 编写这些属性”:

public class MyMath
{
    public static double Pi 
    {
       get {return 3.14;}
    }

    public static int R {get; set;}

    private int MyPrivateProp1 {get; set;}

    public double MyCalcMethod()
    {
           return MyPrivateProp1 * R;
    }
}

我想像这样使用这个类:

var x = MyMath.Pi * MyMath.R;

提前感谢您的宝贵时间。

【问题讨论】:

标签: c# javascript oop


【解决方案1】:

您可以创建一个self-executing 函数来创建您的对象,这将允许您立即从您的 Javascript 调用变量和方法,而无需创建对象的实例。

例如:JSFiddle

var myMath = (function MyMath() {
    this.Pi = 3.14;
    this.R = 0;

    var MyPrivateProp1 = 15;

    this.MyCalcMethod = function() {
      return R * MyPrivateProp1;
    };

    return this;
})();

myMath.R = 5;

var x = myMath.Pi * myMath.R;

console.log(myMath.Pi);
console.log(myMath.R);
console.log(myMath.MyPrivateProp1); //This is returned as Undefined because it is a Private Variable to the Object.
console.log(myMath.MyCalcMethod());
console.log(x);

注意函数末尾的return this,这是确保将对象传递给myMath变量所必需的。

【讨论】:

    【解决方案2】:

    答案是闭包。

    var Person = function () {
        var localVariable = "I'm hidden";
    
        this.publicProperty = "example";
    
        this.publicFunction = function () {
            // This function has access to the localVariable
            // but nothing outside can get to it directly.
            console.log(localVariable);
            return "aren't I clever?";
        }
    };
    
    Person.prototype.staticProperty = "A banana";
    

    现在你可以创建你的 person 类的实例了:

    var aPerson = new Person();
    console.log(aPerson.publicProperty);
    console.log(aPerson.publicFunction());
    console.log(aPerson.staticProperty);
    console.log(aPerson.localVaraible) // undefined.
    

    【讨论】:

      【解决方案3】:

      您可以创建一个对象来存储这两个变量。

      例子:

      var MyMath = {}; //Create a new object
      MyMath.Pi = 3.14;
      MyMath.R = function(value){
          if(value !== undefined){
              this.val=value;
          }// Set
              else return this.val //Get
      
      };
      

      你可以随心所欲地使用它:

      var x = MyMath.Pi * MyMath.R(5);
      

      【讨论】:

        【解决方案4】:

        试试这个代码:

          // define the MyMathClass
            function MyMath() {
                function MyPrivateProp1(){
                //Define get set;
                }
            }
            MyMath.prototype.Pi  = function(){
            //Define get
              return 3.14;
            };
        
            MyMath.prototype.R = function(){
            //Define get and set
            };
        
        
            var MyMath = new MyMath();
            var x = MyMath.Pi() * MyMath.R();
        

        参考资料。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide http://javascriptissexy.com/oop-in-javascript-what-you-need-to-know/

        【讨论】:

          猜你喜欢
          • 2018-05-09
          • 2013-10-28
          • 1970-01-01
          • 2017-02-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-23
          • 2017-06-27
          相关资源
          最近更新 更多