Javascript:默认原型字段,显示使用空构造函数创建的对象的未定义字段
我注意到下面代码中的一些行为,我无法解释。我希望有人能告诉我为什么会这样 如果JSFIDLE更易于阅读,那么下面的代码示例也可以在JSFIDLE上使用。 代码:Javascript:默认原型字段,显示使用空构造函数创建的对象的未定义字段,javascript,oop,object,constructor,prototype,Javascript,Oop,Object,Constructor,Prototype,我注意到下面代码中的一些行为,我无法解释。我希望有人能告诉我为什么会这样 如果JSFIDLE更易于阅读,那么下面的代码示例也可以在JSFIDLE上使用。 代码: function Book() { }; function Book (title, author) { this.title = title; this.author = author; }; Book.prototype = { title: "", ISBN: "isbn", length: 1
function Book() {
};
function Book (title, author) {
this.title = title;
this.author = author;
};
Book.prototype = {
title: "",
ISBN: "isbn",
length: 100,
genre: "genre",
covering: "covering",
author: "author",
currentPage: 0,
toString: function toString() {
console.log("toString Function");
console.log('Title: ' + this.title + '\n' +
'ISBN: ' + this.ISBN + '\n' +
'length: ' + this.length + '\n' +
'genre: ' + this.genre + '\n' +
'covering: ' + this.covering + '\n' +
'author: ' + this.author + '\n' +
'currentPage: ' + this.currentPage + '\n');
}
};
var book1 = new Book();
book1.toString();
var book2 = new Book("First edition", "Random");
book2.toString();
book1.toString()的输出
toString函数
标题:未定义
ISBN:ISBN
长度:100
类型:类型
覆盖:覆盖
作者:未定义
当前页面:0
book2.toString()的输出
toString函数
标题:第一版
ISBN:ISBN
长度:100
类型:类型
覆盖:覆盖
作者:随机
当前页面:0
我的困惑围绕着作者和标题字段。在空构造函数中,这些字段默认为“未定义”,而不是Book类的原型中设置的字段
感谢所有帮助这件事的人
干杯,Cam。
JavaScript
不像Java
(或其他编程语言)那样,如果有不同数量或类型的参数,就可以使用相同名称的方法或多个构造函数。在本例中,您已将Book()
替换为Book(标题、作者)
要实现您的目标,您可以执行以下操作:
function Book (title, author) {
this.title = title||"";
this.author = author||"";
};
function Book (title, author) {
this.title = title && author ? title : "";
this.author = title && author ? author : "";
};
在这种情况下,标题
和作者
将默认为空的字符串
或者像这样:
function Book (title, author) {
this.title = title||"";
this.author = author||"";
};
function Book (title, author) {
this.title = title && author ? title : "";
this.author = title && author ? author : "";
};
JavaScript
与Java
(或其他编程语言)不同,在Java
(或其他编程语言)中,如果具有不同数量或类型的参数,则可以使用具有相同名称或多个构造函数的方法。在本例中,您已将Book()
替换为Book(标题、作者)
要实现您的目标,您可以执行以下操作:
function Book (title, author) {
this.title = title||"";
this.author = author||"";
};
function Book (title, author) {
this.title = title && author ? title : "";
this.author = title && author ? author : "";
};
在这种情况下,标题
和作者
将默认为空的字符串
或者像这样:
function Book (title, author) {
this.title = title||"";
this.author = author||"";
};
function Book (title, author) {
this.title = title && author ? title : "";
this.author = title && author ? author : "";
};
在函数构造函数中定义的属性将始终覆盖在原型上定义的属性 当JavaScript引擎提供原型值时,它将首先在实例上而不是在其原型链上查找它(hasOwnProperty) 由于您在函数构造函数中隐式声明了title和author,new运算符在对象的实例空间中使用这两个属性包装对象,因此对象如下所示:
function Book (title, author) {
this.title = title||"";
this.author = author||"";
};
function Book (title, author) {
this.title = title && author ? title : "";
this.author = title && author ? author : "";
};
书{
标题:未定义,
自动:未定义,
proto:{
标题:“,
ISBN:“ISBN”,
长度:100,
类型:“类型”,
封面:“封面”,
作者:“作者”,
当前页面:0,
原型:对象
}
}
因此,当第一次询问属性时,JavaScript引擎将返回实例上初始化为未定义的参数,因为在使用函数构造函数中定义的new属性创建时传递的参数不会始终覆盖原型上定义的参数 当JavaScript引擎提供原型值时,它将首先在实例上而不是在其原型链上查找它(hasOwnProperty) 由于您在函数构造函数中隐式声明了title和author,new运算符在对象的实例空间中使用这两个属性包装对象,因此对象如下所示:
function Book (title, author) {
this.title = title||"";
this.author = author||"";
};
function Book (title, author) {
this.title = title && author ? title : "";
this.author = title && author ? author : "";
};
书{
标题:未定义,
自动:未定义,
proto:{
标题:“,
ISBN:“ISBN”,
长度:100,
类型:“类型”,
封面:“封面”,
作者:“作者”,
当前页面:0,
原型:对象
}
}
因此,当第一次询问属性时,JavaScript引擎将返回实例上初始化为未定义的参数,因为在初始Book函数中使用new创建时没有传递参数
function Book (title, author) {
this.title = title;
this.author = author;
};
您没有传递任何内容,因此参数进入函数时带有未定义的值。由于您同时声明了title和author,因此您仍在为这两个属性赋值,在本例中为未定义
在Book对象上的第一次查找会命中声明的属性,这两个属性都有一个值,因此您会得到undefined的返回值。由于Book的这两个属性都有一个值,因此查找将停止,并且不会像您最可能期望的那样向原型链上传播
如果希望在不向被调用函数传递参数时设置默认值,则只需将其添加到函数中即可
function Book (title, author) {
this.title = title || "";
this.author = author || "author";
}
在您最初的书中函数
function Book (title, author) {
this.title = title;
this.author = author;
};
您没有传递任何内容,因此参数进入函数时带有未定义的值。由于您同时声明了title和author,因此您仍在为这两个属性赋值,在本例中为未定义
在Book对象上的第一次查找会命中声明的属性,这两个属性都有一个值,因此您会得到undefined的返回值。由于Book的这两个属性都有一个值,因此查找将停止,并且不会像您最可能期望的那样向原型链上传播
如果希望在不向被调用函数传递参数时设置默认值,则只需将其添加到函数中即可
function Book (title, author) {
this.title = title || "";
this.author = author || "author";
}
JS不支持参数多态性,第二本书覆盖了第一本。JS不支持参数多态性,第二本书覆盖了第一本。我明白你的意思了,提图斯。谢谢你的消息。我明白你的意思了,提图斯。谢谢你提供的信息。这真是个好消息