inherit不是Defined

时间:2014-03-09 10:44:36

标签: javascript

我正在学习一本javascript书,我有这个例子

 var p = {
// x and y are regular read-write data properties. x: 1.0,
    y: 1.0,
// r is a read-write accessor property with getter and setter. // Don't forget to put a comma after accessor methods.
    get r() {
        return Math.sqrt(this.x * this.x + this.y * this.y);
    }, set r(newvalue) {
        var oldvalue = Math.sqrt(this.x * this.x + this.y * this.y);
        var ratio = newvalue / oldvalue;
        this.x *= ratio;
        this.y *= ratio;
    },
    // theta is a read-only accessor property with getter only.
    get theta() {
        return Math.atan2(this.y, this.x);
    } };

var q = inherit(p); // Create a new object that inherits getters and setters
q.x = 0; q.y = 0; // Create q's own data properties
console.log(q.r); // And use the inherited accessor properties console.log(q.theta);

但我有这个错误Uncaught ReferenceError: inherit is not defined

3 个答案:

答案 0 :(得分:2)

在互联网上查看此代码表明您可能正在阅读O'Reilly的“JavaScript:权威指南”。如果是,则在示例6-1中给出inherit()的代码: 见这里:https://www.inkling.com/read/javascript-definitive-guide-david-flanagan-6th/chapter-6/creating-a-new-object-that

答案 1 :(得分:1)

直截了当,您错过了第 119 页上的代码(请参阅最后的参考资料),在该代码上定义了继承函数。您似乎将“继承”功能视为“本机功能”,但事实并非如此。如前所述,您需要在初始化之前“构造”该函数 - 这样您就不会得到 Uncaught ReferenceError: inherit is not defined

那个错误告诉你线索:“inherit is not defined”,所以第一步是查找这个函数是在哪里定义的。如果它尚未定义并且您想使用它,您只需要按照下面的建议或您需要播放的特定说明来定义它。如果您正在学习,那么了解错误在困扰着您是非常重要的。

因此,在调用“继承”函数之前,按照书中的建议添加以下代码:

// inherit() returns a newly created object that inherits properties from the
// prototype object p. It uses the ECMAScript 5 function Object.create() if
// it is defined, and otherwise falls back to an older technique.
function inherit(p) {
     if (p == null) throw TypeError(); // p must be a non-null object
      if (Object.create) // If Object.create() is defined...
        return Object.create(p); // then just use it.
     
     var t = typeof p; // Otherwise do some more type checking
     
     if (t !== "object" && t !== "function") throw TypeError();
       function f() {}; // Define a dummy constructor function.
        f.prototype = p; // Set its prototype property to p.
        return new f(); // Use f() to create an "heir" of p.
}

示例 6-1。创建一个继承自原型的新对象。 JavaScript:权威指南,David Flanagan (O'Reilly)。版权所有 2011 大卫弗拉纳根,978-0-596-80552-4。第六版。

答案 2 :(得分:0)

为增加Mchl的答案(我有同样的问题,但无法按照他的答案中的链接进行操作),以下是继承函数的代码,如Javascript The Definitive Guide示例6.1中所示:

// inherit() returns a newly created object that inherits properties from the
// prototype object p.  It uses the ECMAScript 5 function Object.create() if
// it is defined, and otherwise falls back to an older technique.
function inherit(p) {
    if (p == null) throw TypeError(); // p must be a non-null object
    if (Object.create)                // If Object.create() is defined...
        return Object.create(p);      //    then just use it.
    var t = typeof p;                 // Otherwise do some more type checking
    if (t !== "object" && t !== "function") throw TypeError();
    function f() {};                  // Define a dummy constructor function.
    f.prototype = p;                  // Set its prototype property to p.
    return new f();                   // Use f() to create an "heir" of p.