Javascript 为什么我会得到一个';函数未定义';即使我将函数添加到构造函数';什么是原型?
运行以下代码:Javascript 为什么我会得到一个';函数未定义';即使我将函数添加到构造函数';什么是原型?,javascript,Javascript,运行以下代码: console.clear(); var array = [ 'Apples', 'Oranges', , 'Pear', ]; array.first = function() { var len = this.length; if (len === 0) throw new Error("Array empty"); return array[0]; } Array.prototype.last = function() { var len = th
console.clear();
var array = [ 'Apples', 'Oranges', , 'Pear', ];
array.first = function()
{
var len = this.length;
if (len === 0) throw new Error("Array empty");
return array[0];
}
Array.prototype.last = function()
{
var len = this.length;
if (len === 0) throw new Error("Empty array");
return array[len - 1];
}
console.log("========================")
for(prop in array)
console.log(prop + " = " + array[prop].toString());
console.log("========================")
try
{
var first = array.first();
console.log("first = " + first.toString());
}
catch(e)
{
console.log(e.message);
}
try
{
var last = array.last();
console.log("last = " + last.toString());
}
catch(e)
{
console.log(e.message);
}
console.log("========================")
var newArray = [ 'a', 'b', 'c' ];
for(prop in newArray)
console.log(prop + " = " + newArray[prop].toString());
console.log("========================")
try
{
var first = newArray.first();
console.log("first = " + first.toString());
}
catch(e)
{
console.log(e.message);
}
try
{
var last = newArray.last();
console.log("last = " + last.toString());
}
catch(e)
{
console.log(e.message);
}
生成以下输出:
Console was cleared.
========================
0 = Apples
1 = Oranges
3 = Pear
first = function ()
{
var len = this.length;
if (len === 0) throw new Error("Array empty");
return array[0];
}
last = function ()
{
var len = this.length;
if (len === 0) throw new Error("Empty array");
return array[len - 1];
}
========================
first = Apples
last = Pear
========================
0 = a
1 = b
2 = c
last = function ()
{
var len = this.length;
if (len === 0) throw new Error("Empty array");
return array[len - 1];
}
========================
newArray.first is not a function
last is undefined
除了输出的最后一行之外,所有这些都可以,它说,“last未定义。”
为什么即使我将
last
函数添加到Array
class/constructor的原型中,last
函数仍未定义?您的last
方法已添加到原型中并正确执行。这里没有问题
问题是,last
调用返回undefined
,产生的错误是无法读取此行未定义的的属性“toString”:
console.log("last = " + last.toString());
因为last
未定义。发生这种情况的原因是您在此处使用array
而不是This
:
last = function ()
{
var len = this.length;
if (len === 0) throw new Error("Empty array");
return this[len - 1];
}
此外,您还需要将first
方法添加到原型中,以avoildnewArray。first不是函数
错误。您的last
方法已添加到原型中并正确执行。这里没有问题
问题是,last
调用返回undefined
,产生的错误是无法读取此行未定义的的属性“toString”:
console.log("last = " + last.toString());
因为last
未定义。发生这种情况的原因是您在此处使用array
而不是This
:
last = function ()
{
var len = this.length;
if (len === 0) throw new Error("Empty array");
return this[len - 1];
}
您还需要将first
方法添加到原型中,以避免创建newArray。first不是一个函数
错误。非常感谢。:-)是的,first
我故意没有添加到newArray
,只是为了看看会发生什么。我只是想把事情弄得一团糟来学习。:-)非常感谢。:-)是的,first
我故意没有添加到newArray
,只是为了看看会发生什么。我只是想把事情弄得一团糟来学习。:-)值得一提的是,Mozilla说扩展本机原型是一种糟糕的做法:@cs01谢谢。这是可以理解的。无论如何,Mozilla说扩展本机原型是一种糟糕的做法:@cs01谢谢。这是可以理解的。