【问题标题】:A javascript function that has a defined function具有已定义函数的 javascript 函数
【发布时间】:2014-04-21 03:16:47
【问题描述】:

我正在尝试让您更好地了解 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 has no method '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
}

【问题讨论】:

  • 您应该发布完整的代码。我猜你应该使用 myTestObject.prototype.type = function() { } 来定义类型,但是类型函数也可能有问题。

标签: javascript


【解决方案1】:

我会使用这样的东西:

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."

【讨论】:

  • 这将是我使用 box 对象的首选方法。但是,如果您需要调用示例中描述的方法并允许链接,该怎么办。这有可能吗?
【解决方案2】:

这可以满足您的要求,但我不明白您想对原型做什么,所以这段代码没有使用它。例如,示例代码没有使用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

【讨论】:

  • 感谢您的帮助。我试图看看是否可以使用原型实现相同的功能。然而,经过进一步研究,如果不调整呼叫程序,似乎也不可能。我在上面添加了我试图实现的非工作代码。我会将你的答案标记为正确。
  • 我还有一个问题。使用您的代码为什么会引发错误。 var myTestObject = someObject().type("someType");
  • 那个 sn-p 应该可以自己运行,但它不会再设置myTestObject。我的示例将myTestObject 设置为formatter,但这会将其设置为.type 方法的返回值。当.type 被调用时,它不会返回任何值,所以myTestObject 将被设置为undefined,并且不可能在undefined 上调用方法。也许这也是您的新示例的问题,box() 返回一个字符串,但看起来您想将其用作构造函数,因此调用 new box() 并且不返回任何内容。
  • Box.prototype._type = "initialType" 不会比this._type = "initialType" 更好吗?无需在每个实例中存储它。
  • 是的,可以。我第一次看到你的评论时看起来有点奇怪,但我想不出有什么理由不这样做。
猜你喜欢
  • 2011-04-23
  • 2017-06-29
  • 1970-01-01
  • 1970-01-01
  • 2023-01-19
  • 2011-06-06
  • 2012-03-09
  • 1970-01-01
  • 2014-08-26
相关资源
最近更新 更多