我正在尝试在此示例中将原型添加到已创建的对象实例(Person1),但它未成功添加,从而产生错误。有人可以帮我吗?
import pandas as pd
import datetime as dt
date=dt.date(2018,1,18)
purchase_1 = pd.Series({'Store':'Store 1','Date': date,
'Item Purchased': 'Dog Food',
'Cost': 22.50,'Client':'Mark'})
purchase_2 = pd.Series({'Store':'Store 2','Date': date,
'Item Purchased': 'Kitty Litter',
'Cost': 2.50,'Client':'Den'})
purchase_3 = pd.Series({'Store':'Store 3','Date': date,
'Item Purchased': 'Bird Seed',
'Cost': 5.00,'Client':'Max'})
df = pd.DataFrame([purchase_1, purchase_2, purchase_3])
df = df.set_index(['Store', 'Date'])
df.index.names = ['Store', 'Date']
Store Date Client Cost Item Purchased
Store 1 2018-01-18 Mark 22.5 Dog Food
Store 2 2018-01-18 Den 2.5 Kitty Litter
Store 3 2018-01-18 Max 5.0 Bird Seed
答案 0 :(得分:1)
问题是person
是一个函数而person1
是一个对象。使用constructor.prototype
解决问题。
<html>
<head></head>
<body>
<script>
var person = function(name, age){
this.name = name;
this.age = age;
this.printName = function(){
alert(this.name);
}
}
person.prototype.printAge = function(){
alert(this.age);
}
var person1 = new person("Jack", 29);
person1.constructor.prototype.printMessage = function(){
alert("Hello Friend");
}
var person2 = Object.create(person1);
person2.printName();
person2.printAge();
person2.printMessage();
</script>
</body>
</html>
&#13;
答案 1 :(得分:0)
只有构造函数具有protoype属性(通过this.propertyName设置属性值的函数。虽然你可以使用
__proto__
它被认为效率低下。
检查一下。