具有已定义函数的javascript函数

时间:2014-03-15 19:23:06

标签: javascript

我正在尝试让您更好地理解JavaScript,尤其是原型功能。我遇到这个案子有问题:

我正在尝试使用类型函数定义一个函数someObject,以便它的行为类似于以下内容:

var myTestObject = someObject();

如果我打电话:

myTestObject() ===> "The object is initailType"

然后当它被称为

myTestObject.type() ===> "InitialType"

然后,如果我打这个电话

myTestObject.type("newtype") 
myTestObject.type() ===> "newType"

致电

myTestObject() ===> "The Object is newType".

我已尝试过这两个How does JavaScript .prototype work?

How do you create a method for a custom object in JavaScript?

,但是我会得到几个不同的错误,具体取决于它是如何实现的,主要是这个(Uncaught TypeError:Object myTestObject没有方法' type')。我觉得我正在努力做到这一点。

编辑:更多代码。

function box(){
    var _current = "initialType"
    Object.defineProperty(this, "current", {
        get: function(){return _current;},
        set: function(value){
            if(arguments.length === 1){
                _current = value;
            } }
    })
    return "The Object is " + this.type(this.current)
}

box.prototype.type = function(newValue){
    var type = null;
    if(arguments.length == 0){
        type = "initialType";
    }else {
        type = newValue
    }
    return type
}

2 个答案:

答案 0 :(得分:2)

我会用这样的东西:

function Box(){}
Box.prototype.type = "initialType";
Box.prototype.toString = function() {
    return "The Object is " + this.type + ".";
};

并像这样使用它:

var b = new Box();
b.type;               // "initialType"
b + '';               // "The Object is initialType."
b.type = 'otherType'; // "otherType"
b.type;               // "otherType"
b + '';               // "The Object is otherType."

答案 1 :(得分:1)

这就是您所要求的,但我不明白您想要对原型做什么,所以此代码并未使用它。例如,示例代码不使用new,因此someObject的返回值不会使用其原型。

function someObject()
{
   var currentType = "initailType";
   var formatter = function() {
       return "The object is " + currentType;
   };
   formatter.type = function(value) {
       if (arguments.length == 0) {
           return currentType;
        } else {
           currentType = value;
        }
    };
    return formatter;
}

var myTestObject = someObject();
myTestObject();      // => "The object is initailType"
myTestObject.type(); // => "initialType"
myTestObject.type("newType");
myTestObject.type(); // => "newType"
myTestObject();      // => "The object is newType".

see demo

编辑:使用原型和新的示例。

function Box() { // class name starts with a capital letter
    this._type = "initialType"; // set up default values in constructor function
} // no "return" in constructor function, using "new" handles that

Box.prototype.type = function(value) { // adding method to the prototype
    if (arguments.length == 0) { // magic arguments local variable...
        return this._type; // initially returns the value set in the constructor
    } else {
        this._type = value; // update the stored value
    }
};

Box.prototype.format = function() // another method on the box, rather than a return from the constructor
{
    return "The object is " + this.type(); // could use this._type instead
};

var box = new Box();       // instance variable with lowercase name
console.log(box.type());   // read the default value
console.log(box.format()); // print the message with the initial value of type
box.type("another type");  // set the type property, no return value
console.log(box.format()); // print the new message