我正在做一个Javascript课程的介绍,需要一些最佳实践和一些总体反馈的帮助,因为我的老师似乎对自从他任职以来帮助我们的学生不感兴趣。Object.create和init
完整免责声明:代码在编译下,并且是正确(从教授提供的测试用例中判断)。因此,我不需要实际任务的帮助,但是如何解释与运行背后的create()和任何(如果有的话)逻辑捆绑在一起的init的用例。
/**
* Exercise 2.2
*
* Create a method 'shape.init(x, y, height, width)' that helps you to
* initiate a object. Try it out by re-initing 'shape1' using the method.
*
* Answer with 'shape1.print()'.
*
* Write your code below and put the answer into the variable ANSWER.
*/
function shape(){
this.x = 0;
this.y = 0;
this.height = 0;
this.width = 0;
}
shape.print = function(){
return 'x:' + this.x + ', ' +
'y:' + this.y + ', ' +
'height:' + this.height + ', ' +
'width:' + this.width;
}
var shape1 = Object.create(shape, {x : {value : 29},
y : {value : 56},
height : {value : 17},
width : {value : 19}});
shape.init = function(x, y, height, widht){
function shape(x, y, height, width){
this.x = x;
this.y = y;
this.height = height;
this.width = width;
}
}
shape1.init(29, 56, 17, 19);
ANSWER = shape1.print();
我有困难的时候,下面是你为什么会需要的时候可以使用的Object.create一个初始化函数()(这在我的心灵的作品一样的INIT)...
老师在这一点上只是一无所知,或者是否存在这样的情况:实现已经使用object.create()初始化对象的init是否值得呢?
我认为你需要更具体一点。我不确定哪些位需要澄清。部分原因可能是因为这不像JS成语。 – ssube
那么,我不确定的是,为什么我们在任何情况下都需要一个init,我们基本上使用object.create()。对我而言,他们是独一无二的;一个初始化老派对象(java中的'classes'),另一个通过对已经定义的对象的引用来初始化对象,并且它的属性... – geostocker
'init'函数似乎不正确。它不会返回任何东西。 –