【问题标题】:Can not extend this JavaScript object无法扩展此 JavaScript 对象
【发布时间】:2019-02-18 08:57:39
【问题描述】:

我检查了一些有类似问题的问题,但无法处理我有 file.js 的问题:

'use strict'

function singlyLinkedList() {
  if (this ) {
    this.head = null
  }
}

singlyLinkedList.prototype.append = function(value) {
  let node = {
    data: value,
    next: null
  }
  if( !this.head ) {
    this.head = node
  } else {
    let pointer = this.head
    while( pointer ) {
      pointer = pointer.next
    }
    pointer.next = node
  }
}

我从 index.html 调用:

<!DOCTYPE html>
<html>
  <head>
    <title> Test </title>
    <meta charset="UTF-8">
    <script src="file.js"></script>
  </head>
  <body>
    <script>
      let linkedList = singlyLinkedList()
      let integersArray = [1, 22, 333, 4444]
      integersArray.forEach(element => linkedList.append(element))

    </script>
  </body>
</html>

使用 Chrome 浏览器浏览此 HTML 文件并检查控制台,显示以下错误消息:

未捕获的类型错误:无法读取未定义的属性“追加”

如何解决这个问题?

更新:

我遇到的第二个问题(可能是一个单独的问题?)是,如果我写:

function singlyLinkedList() {
    this.head = null
}

我收到此错误消息:

未捕获的类型错误:无法设置未定义的属性“头”

【问题讨论】:

  • singlyLinkedList 没有返回任何内容。如果你想要一个实例,请使用new
  • 或者,你可以从singlyLinkedList返回this
  • @Tzelon 我尝试了你的想法,但在删除了if 声明并做了:this.head = null 然后return this.head 但我得到Uncaught TypeError: Cannot set property 'head' of undefined
  • 我认为你需要像let linkedList = new singlyLinkedList()一样使用它
  • @CertainPerformance 是对的。您需要使用“新”来获取实例。对不起

标签: javascript function prototype strict


【解决方案1】:

您需要注意的几件事,

  1. 使用new 关键字创建“singlyLinkedList”实例
  2. while 循环的终止条件不正确。应该是while( pointer.next )

检查以下版本,

//create a `file.js` file and put this code inside that. running this code snippet on stackoverflow util wont work as you need a separate `file.js`

'use strict';

function singlyLinkedList() {
    this.head = null;
}

singlyLinkedList.prototype.append = function(value) {
    let node = {
        data: value,
        next: null
    };
    if( !this.head ) {
        this.head = node
    } else {
        let pointer = this.head;
        while( pointer.next ) {  //check this
            pointer = pointer.next
        }
        pointer.next = node
    }
};
<!DOCTYPE html>
<html>
  <head>
    <title> Test </title>
    <meta charset="UTF-8">
    <script src="file.js"></script>
  </head>
  <body>
    <script>
        let linkedList = new singlyLinkedList(); // check this
        let integersArray = [1, 22, 333, 4444];
        integersArray.forEach(element => linkedList.append(element));
        console.log('linkedList: ', linkedList);

    </script>
  </body>
</html>

它会记录类似的东西,

而且我坚信您需要使用new 关键字来创建singlyLinkedList 函数的实例,因为您想使用prototype 概念的好处

【讨论】:

  • 谢谢你,但那是我一开始使用new的方式(抱歉没有提及)
  • 那么问题出在哪里?
  • 这是我分享的第一个:Uncaught TypeError: Cannot read property 'append' of undefined。您是否按照我的方式运行您的解决方案,即将 JS 代码保存在单独的文件中,然后从 HTML 文件中调用它?
  • 是的,它工作得很好,并在控制台中记录了所需的输出,让我更新答案
  • 奇怪...我同时使用 Firefox 和 Chrome...你在我的代码中是否也使用了“严格模式”?
猜你喜欢
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 2011-09-21
  • 2012-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多