Javascript 有个问题我不知道';我不明白,有人能理解吗?
在线资源文件中的问题:Javascript 有个问题我不知道';我不明白,有人能理解吗?,javascript,Javascript,在线资源文件中的问题: 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());
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
有人能理解他们在问什么吗
以下是我的评论的副本,格式很好:
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);
好吧,我已经玩过了,大致了解了正在发生的事情的要点,尽管我不确定什么时候或者为什么需要用这种技术来写作;如果有人能解释一下,我也许会对它的用途有更好的了解
我的最终代码如下:-
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);
我不能完全确定我是否正确地使用了Circle.prototype.toString=function(),因为它似乎只是创建了一个字符串。他们要求您创建一个名为
Circle
的构造函数,它创建的对象将具有get\u area
,set\u radius
,和toString
功能,这些功能以指定的方式运行。(toString
将在final语句中使用,您将输出circle
实例的值。)他们可能希望您通过circle
原型为对象提供这些功能(见下文)
Circle
是一个构造函数,因为它创建新对象(从技术上讲,它只是填充它们;它们是由new
操作符创建的)。有时人们调用这些类,但它们不是真正的基于类的OOP方式的类(它们是JavaScript使用的基于原型的OOP方式的构造函数)
有几种方法可以做到这一点,但同样,通常您会有一个循环
函数,该函数为此
上的实例设置属性,然后将函数分配给循环。原型
,通过新建循环
创建的所有对象都将继承该循环。我会匆匆举个例子,但我的印象是这是一个学习练习,所以最好留给读者。但也有一些建议:
- 在构造函数中,您可以使用关键字
引用this
操作符创建的对象new
- 构造函数对象将有一个名为
的属性。如果您将属性分配给该原型,则它们将由使用构造函数创建的实例继承(通过称为原型链的东西继承)。因此,如果我有一个构造函数prototype
,我可以在Foo
(Foo.prototype
)上设置一个属性(比如,Foo.prototype.bar=/*无论什么*/;
),通过bar
创建的所有实例都将具有该值的new Foo()
属性bar
- JavaScript中的函数是一级对象。您可以引用它们,就像引用字符串或数字一样。如果我有
,我可以设置一个变量函数foo(){/*…*/}
来引用它(x
),然后通过该变量调用它(var x=foo;
)x();
- 就像
可以引用函数一样,我上面提到的x
属性也可以引用函数bar
- 我发现David Flanagan(来自O'Reilly)的《JavaScript:权威指南》一书非常好。它可能有点过时了,第五版已经有好几年的历史了
- 在上有一大堆备受尊敬的JavaScript大师道格拉斯·克罗克福德的文章,但我警告您克罗克福德很简洁。:-)尽管一个人并不总是同意他的结论,但他还是很聪明,见多识广
- 我真的不喜欢看我的博客[因为A)这么说并不是为了自我推销,B)“贫血”并没有说一半]但我认为这是有原因的
// 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).
(该示例使用匿名函数,我很抱歉,但我不想在这里讨论命名函数。)
(OT:挑剔的是,向您提出这个问题的人真的应该在一开始就声明
circle
变量:var circle;
另外,请注意console.log
可能并不存在于所有JavaScript实现中——这是从Firefox+Firebug中最为人所知的。)我想我们要问的是为Circle类实现一个toString()
函数(或JS中的任何等价函数),它将查找this.get\u radius()
和this.get\u area()
,并输出一个格式良好的字符串:“我的圆的半径…”您应该实现javascript函数,用get_area()和set_radius()函数创建一个圆圈对象,以允许脚本运行,如图所示。您需要用javascript编写“圆圈”类,以便:
默认情况下,其面积为PI(3.141592653…)或22/7
圆对象的半径属性最初为1,因为圆的面积=PI*R*R
将半径设置为10,则公式PI*10*10=314.1592653
我想这很简单,现在你可以自己编写javascript代码了。这超出了我对javascript的理解,但我当然愿意尝试一下并理解它。为了更好地理解这一点,我可以在网上查阅一些资料吗?@user275074:是的,对不起,我一直在试图澄清这一点,并得出结论说我没有取得任何进展。我已经添加了一些参考资料供您查看,因为您可以在其他地方找到一个,所以我将添加一个示例。我试着不为你做这件事而把你搞得一团糟,但我认为我犯了一个错误,那就是做得不够。我把下面这些东西放在一起,是不是走对了方向:-功能圈(){