为什么用继承覆盖构造函数字段

时间:2017-09-08 11:30:05

标签: javascript inheritance

为什么我们需要在继承后将import json def convertMatrix(file_name): final_list = [] try: with open(file_name, "r") as sparse_mat: matrix = sparse_mat.readlines() for row in matrix: each_dct = {} row_vals = row.rstrip("\n").split() if row_vals: each_dct["Flag"] = row_vals[0] for col_elem in row_vals[1:]: if ":" in col_elem: each_col_val = col_elem.split(":") column_name = "Col"+each_col_val[0] column_val = str(each_col_val[1]) each_dct[column_name] = column_val final_list.append(each_dct) except Exception as e: print "Exception occured", e return final_list print convertMatrix("demo.text") 属性设置为Child.prototype.constructor。例如

Child

正如我所看到的,构造函数的实例在没有function Parent(a){ this.a = a; } function Child(a, b){ Parent.call(this. a); this.b = b; } Child.prototype = Object.create(Parent.prototype); Child.prototype.constructor = Child // 1)**

的情况下是相同的

为什么我们需要此// 1) **行?

1 个答案:

答案 0 :(得分:0)

因为它不应该是相同的

语言语法Child.prototype.constructor应该是Object.create(Parent.prototype).constructor的实例,实际上是Parent.prototype.constructor

然后由引擎实现或优化。但它不能信任,因此// 1)行确保它