我有一个人员类,该人员类具有User和Admin的子类,User将创建用户实例... 现在,通过检查arguments.length来验证我的addPerson()属性无效。
即使我可以记录arguments.length
,if语句也不会执行var Person = function(name, email) {
this.name = name;
this.email = email;
this.addPerson = function(name, email) {
var paraLength = arguments.length;
console.log(paraLength) //logs 2
if(paraLength < 2 || > 2) {
return "Input must be just name and email"; //does nothing
}else{
//do other things
}
}
const User = function(name, email) {
Person.call(this, name, email);
this.addPerson(name, email); //adding user on execution
};
User.prototype = Object.create(Person.prototype);
//Pointing User constructor to itself so it can override properties
User.prototype.constructor = User;
var user1 = new User("user1", "email@gmail", "this error", "not just ignored")
如果传入的参数小于或大于两个,我想在return语句上终止。 注意:我打算使用对象原型
答案 0 :(得分:0)
您有一些语法错误-在您的def print_values(self, reverse=False):
values = [val for val in self.__list_generator()]
if values:
print("Linked list: " + str(values))
else:
print("Linked List is currently empty.")
def __list_generator(self):
'''
A Generator remembers its state.
When `yield` is hit it will return like a `return` statement would
however the next time the method is called it will
start at the yield statment instead of starting at the beginning
of the method.
'''
cur_node = self.head_node
while cur_node != None:
yield cur_node.node_value # return the current node value
# Next time this method is called
# Go to the next node
cur_node = cur_node.nextNode
语句中,第二个比较项中缺少def print_values(self, reverse=False):
cur_node = self.head_node
if cur_node:
print('Linked list: ', end='')
while cur_node:
print("'{}' ".format(cur_node.node_value), end='')
cur_node = cur_node.nextNode
else:
print("Linked List is currently empty.")
(应为if
)。您还没有关闭原始的paraLength
函数。并且由于您没有使用|| paraLength > 2
的{{1}}值执行任何操作,因此没有任何反应-使用Person
将和日志返回到控制台:
return
答案 1 :(得分:0)
不确定User.addPerson()
应该做什么,而是回答您的主要问题…
如果您想捕获在new User()
函数中传递给addPerson()
的参数太多(或太少),则可以传递arguments
而不只是名称和电子邮件。您可以执行以下操作:
this.addPerson.apply(this, arguments);
使addPerson
看到传递给User()
的所有参数
var Person = function(name, email) {
this.name = name;
this.email = email;
this.addPerson = function(name, email) {
var paraLength = arguments.length;
console.log(paraLength) //logs 2
if(paraLength < 2 || paraLength > 2) {
console.log( "Input must be just name and email"); //does nothing
}else{
//do other things
}
}
}
const User = function(name, email) {
Person.call(this, name, email);
this.addPerson.apply(this, arguments); //adding user on execution
};
User.prototype = Object.create(Person.prototype);
var user1 = new User("user1", "email@gmail", "this error", "not just ignored")