如何使用“原型”正确编码 JavaScript 属性和方法?功能?
我正在尝试学习如何使用 javascript 原型创建和使用 javascript 属性和方法,但遇到了一些困难。
在下面的代码中,我尝试创建一个名为“radius”的简单对象,其半径为 4,并具有一个名为“getCircumference”的方法,该方法使用对象“半径”值生成周长。
function radius()
{
this = 4;
}
function getCircumference()
{
var pi = 3.141592;
var circumference = this.value * 2 * pi;
document.write(circumference);
}
radius.prototype.circumference = getCircumference;
radius.circumference();
如果有人可以向我展示如何使该代码工作,更好的是,如何使该代码允许输入任何半径并在每次调用新方法时返回周长。谢谢!
I am trying to learn how to create and use javascript properties and methods using javascript prototypes and am having a bit of difficulty.
In the following code I am trying to create a simple object called 'radius' that has a radius of 4 and has a method called 'getCircumference' which produces the circumference using the object radius' value.
function radius()
{
this = 4;
}
function getCircumference()
{
var pi = 3.141592;
var circumference = this.value * 2 * pi;
document.write(circumference);
}
radius.prototype.circumference = getCircumference;
radius.circumference();
If someone can show me how to make this code work, and better yet, how to make this code allow the input of any radius and return the circumference each time the new method is called upon. thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码在技术上和概念上都存在一些问题。
从技术角度来看,您无法分配给
this
。将该行更改为this.value = 4
并将其与 pst 的建议(将最后一行更改为(new radius()).curcumference()
)我相信您的代码将按原样运行。从概念上讲,将圆视为一个对象并具有半径可能比将半径本身视为一个对象更有用。试试这个:
There are a couple of problems with your code, both technically and conceptually.
On the technical side, you can't assign to
this
. Change that line tothis.value = 4
and combine it with pst's suggestion (change the last line to(new radius()).curcumference()
) and I believe your code will work as-is.Conceptually, it's probably more useful to think of a circle as an object and as having a radius rather than thinking of the radius itself as an object. Try this:
尝试:
(new radius).circumference()
——“parent”函数的“.prototype”属性中的对象被放入“[[prototype]]”链中(关键字提示: -) 对于通过 new 派生的对象。如果您的构造函数接受一个参数,那么您可能可以这样:
new radius(42).circumference()
。有关详细信息,请参阅 http://jibbering.com/faq/notes/closures/。
Try:
(new radius).circumference()
-- the object in the ".prototype" property on the "parent" function is put into the "[[prototype]]" chain (keyword hint :-) for objects derived from it vianew
.If your constructor took an argument, then you could this perhaps:
new radius(42).circumference()
.See http://jibbering.com/faq/notes/closures/ for details.
让我们首先了解一些原型基础知识:
如果您习惯了面向对象的编程语言,您可以将原型视为类定义/静态变量集的组合。当然,无论您选择什么类比都会有缺陷,因为它不等同于任何一个。
当您在 JavaScript 中定义任何函数时,您有可能创建该函数的实例(类似于 OOP 中的
对象
)。考虑以下示例:
如果在窗口上下文中调用它:
您就会知道
foo
是一个临时变量,在持续时间内存储'fizz'
的值函数调用的值,并且window.bar
将被设置为'buzz'
的值。但是,如果它用于实例化一个新对象,
您就会知道
foo
仍然是一个临时变量,在函数调用期间存储'fizz'
的值,现在temp.bar
将设置为'buzz'
的值。如果我们要在原型上定义一个新属性:
example
函数的每个实例现在都会有一个属性baz
,其值为'w00t'.
您可以将函数定义为原型的属性,以提供“类”方法:
并且
example
的所有实例都将具有为其定义的doSomething
函数。当您想要覆盖原型值时会发生什么?
如果您覆盖
原型
上的属性,则所有实例都将被更新:但是,如果您覆盖实例上的属性,仅该实例将被更新:
您的示例:
您想要一个具有半径的
circle
对象:每个
circle
对象都有一个周长:现在,如果实例化一个新圆,您可以获得周长:
快速说明:
定义一个函数原型上允许所有实例共享对同一函数的引用。
如果为每个实例创建一个函数:
您的性能可能会比允许实例共享原型函数更差:
Lets first understand some of the prototype basics:
If you're used to an object-oriented programming language, you can think of
prototype
as a combination class definition/static variable set. Of course, whatever analogy you choose will fall short, as it's not equivalent to either.When you define any function in JavaScript, you have the potential to create an instance of the function (similar to an
object
in OOP).Consider the following example:
If it was called in the context of the window:
you'd know that
foo
was a temporary variable storing the value of'fizz'
for the duration of the function call, and thatwindow.bar
would be set to the value of'buzz'
.However, if it were used to instantiate a new object
you'd know that
foo
was still a temporary variable storing the value of'fizz'
for the duration of the function call, and nowtemp.bar
would be set to the value of'buzz'
.If we were to define a new property on the prototype:
Every instance of the
example
function will now have a propertybaz
with a value of'w00t'
.You can define functions as properties of prototype to provide your "Class" methods:
And all instances of
example
will have thedoSomething
function defined for them.What happens when you want to override a prototype value?
If you override a property on the
prototype
all instances will be updated:However, if you override a property on the instance, only that instance will be updated:
Your example:
You want a
circle
object that has a radius:Every
circle
object has a circumference:Now if you instantiate a new circle, you can get the circumference:
Quick Note:
defining a function on a prototype allows all instances to share a reference to the same function.
If you create a function for every instance:
You will likely have worse performance than allowing the instances to share the prototype function: