JavaScript对象和原型 [英] JavaScript Object and Prototypes

查看:72
本文介绍了JavaScript对象和原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript对我来说是一个非常奇怪的地方...


所以,我决定,在我尝试创建一个Object之前,我应该首先了解Objects。我之前在教程的帮助下创建了一个。该教程很复杂,因为向我展示了如何创建一个JavaScript对象,它是一个表示我的.NET对象,以及如何使两者一起工作......显然教程不是很清楚什么是在JavaScript对象中...甚至我对JavaScript对象的广泛评论我仍然没有真的知道发生了什么...当时我对.NET概念比对教程中使用的JavaScript技术更感兴趣。


无论如何,我从基础开始:我如何创建一个JavaScript对象。

令我惊讶的是所有函数都是对象,所有对象都是函数,我对这种新思维方式并不满意,因为它真的很奇怪......但是事情就是这样。


我接下来要做的就是弄清楚我在教程中创建JavaScript对象的工作。此Object使用prototype属性(显然是一个Object并且是所有函数的属性)来向Object添加函数和属性。


所以我开始玩弄我自己的一个愚蠢的小(简单)对象......


请参阅下面的评论代码我对此感到困惑

JavaScript is a very strange place for me...

So, I decided that, before I attempt to create an Object, I should first learn about Objects. I had actually created one before with the help of a tutorial. The tutorial was complicated because showed me how to create a JavaScript Object that is a representation my .NET Object and how to get the two to "work together as one".... apparently the tutorial was not very clear about what was going on in the JavaScript Object...and even with my extensive commenting of the JavaScript Object I still didn''t "really" know what was going on...at the time I was more interested in the .NET concepts than the JavaScript techniques being used in the tutorial.

Anyways, I started with the basics: how do I create a JavaScript Object.
To my surprise all functions were Objects, all Objects were Functions and I was not pleased with this new way of thinking because it''s really strange...but it''s just how things are.

The next thing I did was try to figure out what I had done to create the JavaScript Object in the tutorial. This Object uses the prototype property (which is apparently an Object and is a property of all functions) to add functions and properties to the Object.

So I started playing around with a silly little (uncomplicated) Object of my own...

Please see the comments in the following code for what I''m confused about:

展开 | 选择 | Wrap | 行号

推荐答案

ok ....让我们仔细看看:) ...


通常你会创建一个这样的构造函数:

ok .... let''s have a closer look at it :) ...

typically you create a constructor like this:

展开 | 选择 | Wrap | 行号


@gits


什么是:的名称我可以这样看一下吗?

@gits


好​​的,覆盖现在对我有意义,谢谢。

@gits


这不是我的要求。


我已经定义了2个版本的Divide()方法:一个在Foo类中,另一个在Foo的原型中。


Foo类中的Divide()方法只返回一个字符串,表明你在Foo的Divide函数中。


Foo'原型中的Divide()方法将a分为a。属于b的财产物业。


我想知道如何在原型中为Foo的实例调用Divide()方法.....不是如何静态调用Divide()原型中的方法......我不知道我是否清楚这里,所以如果你不理解我的问题,我可以尝试重申它。
@gits
What is the name of the ":" notation so that I can look this up?

@gits
Ok, "overwriting" makes sense to me now, thanks.

@gits
This is not exactly what I was asking.

I''ve defined 2 versions of the Divide() method: one in the Foo class and the other is in Foo''s prototype.

The Divide() method in the Foo class simply returns a String stating that "you''re in Foo''s Divide function".

The Divide() method in Foo''s prototype divides "a" property by "b" property.

I''m wondering how to call the Divide() method in the prototype for a live instance of Foo.....not how to statically call the Divide() method in the prototype...I don''t know if I''m being clear here so if you don''t understand my question I can try and restate it.


首先解析原型,然后在创建实例时分配成员变量。当你拥有obj的prototype-property然后直接创建一个成员时,这将删除对该obj的prototypes方法的引用。所以成员将''覆盖''这个实例的原型...你只能静态地调用它

使用原型的
更喜欢因为成员总是在原型时分配内存只阅读和解析一次。


亲切的问候
the prototype is first parsed and later when you create the instance then the member-variables are allocated. when you have a prototype-property for the obj and then create a member directly this will drop the reference to the prototypes method for this obj. so the member will ''override'' the prototype for this instance ... you only could call it staticly

using the prototype is to prefer because members will always alloc memory while the prototype is read and parsed only once.

kind regards


这篇关于JavaScript对象和原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆