来自在线资源文件的问题:
Create javascript so that the following methods
produce the output listed next to them.
circle = new Circle();
console.log(circle.get_area()); // 3.141592653589793
circle.set_radius(10);
console.log(circle.get_area()); // 314.1592653589793
console.log(circle); // the radius of my circle is 10 and its area is 314.1592653589793
Run Code Online (Sandbox Code Playgroud)
谁能理解被问到的是什么?
以下是我的评论的副本,格式相同:
function Circle() {
this.pi=3.141592653589793;
this.radius;
}
Circle.prototype={
get_area: function(){
return this.radius*this.pi;
},
set_radius: function(radius){
return this.radius=radius;
}
};
circle=new Circle();
circle.set_radius(100);
Run Code Online (Sandbox Code Playgroud)
好吧,我已经玩过这个并且有点了解正在发生的事情,尽管我不确定何时或为什么需要用这种技术写作; 如果有人可以解释我可能会更好地了解它的用途.
我的最终代码如下: -
function Circle(r) {
this.pi=Math.PI;
}
Circle.prototype={
get_area: function(){
return this.radius*this.pi;
},
set_radius: function(radius){
return this.radius=radius;
}
};
var circle=new Circle();
circle.set_radius(100);
Circle.prototype.toString=function(){
return 'The radius of my circle is '+circle.radius+' and it\'s area is '+this.get_area();
}
console.log(circle);
Run Code Online (Sandbox Code Playgroud)
我不完全确定我是否正确使用Circle.prototype.toString = function(),因为它似乎只是创建一个字符串.
T.J*_*der 14
他们要求你创建一个构造函数叫做Circle它创建,将有对象get_area,set_radius以及toString在指定的行为方式的功能.(toString将在最终语句中使用,您将输出circle实例的值.)他们可能希望您通过Circle原型为对象提供这些功能(见下文).
Circle是构造函数,因为它创建了新对象(从技术上讲,它只是填充它们;它们是由运算符创建的new).有时人们会调用这些类,但它们并不是真正基于类的OOP方式的类(它们是JavaScript使用的基于原型的OOP方式的构造函数).
有几种方法可以做到这一点,但同样,通常你会有一个Circle函数来设置实例的属性this,然后将函数分配给Circle.prototype,通过它们创建的所有对象new Circle都将继承.我举了一个例子,但我的印象是这是一个学习练习,所以最好留给读者.但有一些指示:
new使用关键字引用运算符创建的对象this.prototype.如果为该原型分配属性,它们将由使用构造函数创建的实例继承(通过称为原型链的东西继承).因此,如果我有一个构造函数Foo,我可以bar在Foo.prototype(Foo.prototype.bar = /* whatever */;)上设置一个属性(比如说),并且通过创建的所有实例new Foo()都将具有一个bar具有该值的属性.function foo() { /* ... */ },我可以设置一个变量x来引用它(var x = foo;)然后通过该变量(x();)调用它.x可以引用一个函数一样,bar我上面提到的属性可以引用一个函数.希望这能让你走上正确的轨道,而不必完全放弃游戏.
重新评论有关JavaScript的更多信息的参考文献:
我的评论如下:真的我不能没有一个例子,因为这样做没有帮助.所以这是构造函数和与之关联的一些函数的示例:
// A constructor function called `Person`
function Person(fname, lname) {
// Within the constructor, we can refer to the new instance via `this`.
// Let's remember the names we were given.
this.firstName = fname;
this.lastName = lname;
// Now, the instance that is returned from `new Person(...)` will have
// those properties on it.
}
// Let's give the Person prototype a function that returns the person's
// full name.
Person.prototype.getFullName = function() {
return this.firstName + " " + this.lastName;
};
// Okay, let's see `Person` in action:
var p = new Person("John", "Doe");
console.log(p.firstName); // "John" -- this is referencing the property we set in the constructor
console.log(p.getFullName()); // "John Doe" -- this calls the function we get from the prototype
// `p` has the `getFullName` function because it inherits it from the
// `Person.prototype` object.
// Let's see what happens if I try to output the instance itself:
console.log(p); // Most like you get "[object Object]" or similar -- not useful!
// In that sort of situation, JavaScript will use a function called `toString`
// to get a string equivalent of the instance. (It's more complicated than that,
// but you don't want me to go into the details here.) So let's give
// `Person.prototype` a `toString` function that does something useful:
Person.prototype.toString = function() {
return this.getFullName();
};
// Note there that we're using `this` again. When a function is called through
// an object property (e.g., `p.toString()`), within the function call `this`
// refers to the object instance. `this` is an important and flexible concept
// in JavaScript and very much unlike its counterpart in (say) Java or C++,
// even though in simple situations (like this) it looks similar.
// Now let's try it:
console.log(p); // "John Doe", because the interpreter implicitly calls `toString` for you
// "Hey, wait a minute!" I hear you saying, "You didn't make a new `p`! How did it
// get the new function?!" The answer is it got it the same way it got `getFullName`:
// from `Person.prototype`. The `p` instance *refers* to the `Person.prototype`, it
// doesn't have a *copy* of it. So changes to `Person.prototype` show up in `p`
// (unless `p` overrides them, which we aren't doing in this example).
Run Code Online (Sandbox Code Playgroud)
(那个例子使用匿名函数,我不喜欢它,但我不想在这里讨论命名函数的整个讨论.)
(OT:挑剔,向你提出这个问题的人真的应该circle在最开始时声明变量:var circle;另外,请注意,console.log在所有JavaScript实现中可能都不存在 - 它最好从Firefox + Firebug中获知.)
| 归档时间: |
|
| 查看次数: |
353 次 |
| 最近记录: |