需要帮助来创建我的第一个javascript类

时间:2013-06-07 09:15:54

标签: javascript class instance default

我被要求根据以下代码创建一个javascript类:

 var var1=new MyClass();

 MyClass.setDefaultYear("2013"); //sets default year for all instances of MyClass to 2013

 var1.getYear(); //returns 2013

 var1.year=2001; //sets year to 2001

 var1.getYear(); //returns 2001

 var var2=new MyClass();

 var2.getYear(); //returns 2013

困扰我的是:MyClass.setDefaultYear("2013")

我已设法让其他所有工作正常但我不明白的是如何使用"MyClass"而不是实例名称。

在自学过程中我错过了什么?

谢谢

6 个答案:

答案 0 :(得分:1)

标识符MyClass是用于创建类实例的函数。由于函数也是一个对象(与其他所有东西一样),它可以具有属性。所以你只需要在对象中添加一个属性:

MyClass.setDefaultYear = function() { ... };

您还需要某处存储默认年份设置。您可以使它也是MyClass对象的成员,或者您可以在创建setDefaultYear方法的代码周围放置一个立即执行的函数表达式,以便您可以将默认年份存储在局部变量中

答案 1 :(得分:1)

未定义MyClass。

function MyClass(){
    this.setDefaultYear = function(year){
        MyClass.prototype.year = year;
    };
    this.getYear = function (){
        return this.year;
    }
}
var var1=new MyClass();
var1.setDefaultYear("2013"); //sets default year for all instances of MyClass to 2013
console.log(var1.getYear()); //returns 2013
var1.year=2001; //sets year to 2001
console.log(var1.getYear()); //returns 2001
var var2=new MyClass();
console.log(var2.getYear()); //returns 2013

答案 2 :(得分:0)

function MyClass(){
// other stuff
}
MyClass.setDefaultYear = function(year){
    MyClass.year = year;
}
// the rest

答案 3 :(得分:0)

简单。将setDefaultYear视为课程功能&在getYear函数中提供默认值作为后备。

MyClass.setDefaultYear = function(year) {
  MyClass.defaultYear = year;
}

MyClass.setDefaultYear("2013");

function MyClass() {
  ....
  ....
  this.year;
  this.getYear = function() {
   return this.year || MyClass.defaultYear; //returns MyClass.defaultYear if this.year is not set.
  };
  this.setYear = function(year) {
   this.year = year;
  };
}

var myClass = new MyClass();
myClass.getYear();//"2013"
myClass.setYear("2015");
myClass.getYear();//"2015"

答案 4 :(得分:0)

我猜你的班级是一个名为MyClass的函数。在JavaScript中,函数是对象。因此,您可以通过关键字this为函数指定属性。此外,每个自定义函数都将由prototype属性进行扩展。此属性是对虚拟对象的引用。如果通过关键字new创建MyClass的新实例,则使用对虚拟对象的引用。您可以通过属性.prototype扩展您的类/虚拟对象。例如:

function MyClass(pname)
{
    this.name = pname;
}

MyClass.prototype.getName = function()
{
    alert(this.name);
}

Obj = new MyClass('Simon');
Obj.getName(); // alerts Simon

答案 5 :(得分:0)

这正是你所要求的。

       /* THIS IS THE "CLASS" */
var MyClass = (function () {

    // MyClass is a function that constructs an instance
    function MyClass() { }

    //Creates a new function on the prototype to get the year for the instance. functions which are part of the prototype are instance functions that are shared across all instances of the object.
    MyClass.prototype.getYear = function () {
        return this.year;
    }

    MyClass.setDefaultYear = function (year) {
        /* The prototype is shared across all instances of MyClass, 
        so all instances receive the same value, unless specified directly on the instance.*/
        MyClass.prototype.year = parseInt(year);
    }


    // return the full MyClass implementation.
    return MyClass;
})(); // Your "class" ends here!

// Tests...

var var1 = new MyClass(); // Create a new instance of MyClass

MyClass.setDefaultYear(2013); // Set The default year for ALL instances of MyClass.

alert(var1.getYear()); // This shows "2013" because that is what has been set on the prototype.

var1.year = 2001; //Year is visible, and you can set it's value for each unique instance.

alert(var1.getYear()); // This displays "2001" because that is the value now belonging to instance "var1"

var var2 = new MyClass(); // Create a new instance of MyClass

alert(var2.getYear()); // Since the prototypes value is still 2013, this displays "2013"