如何通过对象访问oop javascript中的属性,我收到以下错误:“未捕获TypeError:x.firstname不是函数 在newindex.html:19“
如何使用x object
访问firstname function person() {
this.firstName = "hello";
}
var x = new person();
console.log(x.firstname());// how to get firstname from x ??
答案 0 :(得分:1)
df_data.groupby(df_data.id, df_data.type).pivot("date").agg(avg("cost").alias("avg_cost"), first("ship").alias("first_ship")).show()
+---+----+---------------+-----------------+---------------+-----------------+---------------+-----------------+
| id|type|201601_avg_cost|201601_first_ship|201602_avg_cost|201602_first_ship|201603_avg_cost|201603_first_ship|
+---+----+---------------+-----------------+---------------+-----------------+---------------+-----------------+
| 1| B| 3213.0| PORT| 3213.0| DOCK| null| null|
| 2| C| 2321.0| DOCK| null| null| null| null|
| 0| A| 422.0| DOCK| 22.0| PORT| 223.0| PORT|
+---+----+---------------+-----------------+---------------+-----------------+---------------+-----------------+
应该是字母N,并且不带括号。
更改此行:
firstName
到这一行:
console.log(x.firstname());// how to get firstname from x ??
答案 1 :(得分:1)
Firstname是一个属性,而不是一个函数,因此在最后包括括号是创建错误的原因。您也不能匹配变量firstname != firstName
。以下是访问属性和函数的示例。
function Person() {
this.firstName = "hello";
this.firstNameFunc = function() { return 'function exec' };
}
let person = new Person();
console.log(person.firstName);
console.log(person.firstNameFunc());

答案 2 :(得分:0)
firstName
是 property
,而非功能。你应该这样做,
console.log(x.firstName);
样本
function person() {
this.firstName = "hello";
}
var x = new person();
console.log(x.firstName);