无法扩展此 JavaScript 对象
Can not extend this JavaScript object
我检查了一些有类似问题的问题,但无法解决我的问题 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 文件并检查控制台,显示此错误消息:
Uncaught TypeError: Cannot read property 'append' of undefined
如何解决这个问题?
更新:
我遇到的第二个问题(也许是一个单独的问题?)是如果我写:
function singlyLinkedList() {
this.head = null
}
我收到此错误消息:
Uncaught TypeError: Cannot set property 'head' of undefined
您需要注意的几件事,
- 使用
new
关键字创建 'singlyLinkedList' 的实例
- 你的
while
循环的终止条件不正确。应该是 while( pointer.next )
检查以下版本,
//create a `file.js` file and put this code inside that. running this code snippet on Whosebug 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
概念的好处
我检查了一些有类似问题的问题,但无法解决我的问题 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 文件并检查控制台,显示此错误消息:
Uncaught TypeError: Cannot read property 'append' of undefined
如何解决这个问题?
更新:
我遇到的第二个问题(也许是一个单独的问题?)是如果我写:
function singlyLinkedList() {
this.head = null
}
我收到此错误消息:
Uncaught TypeError: Cannot set property 'head' of undefined
您需要注意的几件事,
- 使用
new
关键字创建 'singlyLinkedList' 的实例
- 你的
while
循环的终止条件不正确。应该是while( pointer.next )
检查以下版本,
//create a `file.js` file and put this code inside that. running this code snippet on Whosebug 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
概念的好处