关于在自定义对象上创建属性的问题 [英] Question about making a property on custom object

查看:76
本文介绍了关于在自定义对象上创建属性的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!


我最近对JavaScript基于原型的

面向对象很感兴趣。

但是,我仍然不清楚这个机制。


[Q1]

以下两个有什么区别?


(1)

函数C(){

this.name =" Unknown"

}


(2)

函数C(){

}

C.prototype.name = 未知


[Q2]

JavaScript中没有类的概念,一切都是对象。

在以下代码中创建了多少个对象,包括在后面隐式创建的

对象?


函数C(){

}


函数D(){

}

D.prototype = new C

var obj = new D

[Q3]

我看到一些代码如this.base = SomeClass。

什么时候需要ed?

它与ClassName.prototype = new ParentClassName不同吗?

[Q4]

原型和__proto__之间有什么区别? ?

提前致谢。

Sam

Hello!

I got recently intrigued with JavaScript''s prototype-based
object-orientation.
However, I still don''t understand the mechanism clearly.

[Q1]
What''s the difference between the following two?

(1)
function C(){
this.name = "Unknown"
}

(2)
function C(){
}
C.prototype.name = "Unknown"

[Q2]
There''s no notion of Class in JavaScript and everything is an object.
How many objects are created in the following code including the
objects implicitly created in the behind?

function C(){
}

function D(){
}
D.prototype = new C

var obj = new D
[Q3]
I saw some codes like "this.base = SomeClass".
When is it needed?
Is it different from ClassName.prototype = new ParentClassName?
[Q4]
What''s the difference between prototype and __proto__?
Thanks in advance.
Sam

推荐答案

Sam Kong写道:
Sam Kong wrote:
[Q1]
以下两个有什么区别?

(1)
函数C() {
this.name =" Unknown"
}

(2)
函数C(){
}
C.prototype .name ="未知


使用(1),创建的对象拥有该属性。意味着通过相同的构造函数创建的对象

可能仍然具有不同的属性

和属性值(如果它们之后被修改)。


使用(2),创建的对象的原型具有属性,因此

对象通过原型链继承该属性。意思是

如果你修改了prototype属性/方法`name'',那么所有对象都通过这个没有'name''属性的构造函数创建了



在某种意义上修改了他们的`name''属性,因为在这种情况下,

原型链是在属性访问时访问的。


以前经常对此进行更详细的解释,请在发布之前搜索档案[1]。

[Q2]
这里有JavaScript中没有Class的概念,一切都是对象。


不正确。 JavaScript中没有针对HTML实现的类概念用户代理,并且不仅有对象而且还有原始值。

在以下代码中创建了多少个对象包括隐含在后面创建的
对象?

函数C(){
}

函数D(){
}
D.prototype = new C

var obj = new D


四个很明显。每个由C和D引用的两个Function对象,

和D.prototype和obj各引用的两个Object对象,其中D

对象(对象的缩写)通过D构造函数创建是Object

继承自C.prototype的对象。


window.alert([C,D,D.prototype, obj]);


其他隐式创建的对象包括全局对象和所有其他

核心对象。详见ECMAScript 3. [2]

[Q3]
我看到一些代码如this.base = SomeClass。
什么时候需要?


当你想(重新)定义要创建的对象的属性时,

调用对象或全局对象;哪一个取决于执行

上下文。

它与ClassName.prototype = new ParentClassName不同吗?


与此完全不同。后者使ParentClassName

对象成为ClassName对象的原型对象,因此使ClassName

对象继承自ParentClassName.prototype。

[Q4 ]
原型与__proto __有什么区别?
[Q1]
What''s the difference between the following two?

(1)
function C(){
this.name = "Unknown"
}

(2)
function C(){
}
C.prototype.name = "Unknown"
With (1), the created object owns the property. Meaning that objects
created through the same constructor may still have different properties
and property values if they are modified afterwards.

With (2), the prototype of the created object has the property, so the
object inherits the property through the prototype chain. Meaning that
if you modify the prototype property/method `name'', all objects created
through this constructor that do not own a `name'' property themselves,
in a sense have their `name'' property modified as in that case the
prototype chain is accessed on property access.

This has been explained in more detail very often before, please search
the archives[1] before you post.
[Q2]
There''s no notion of Class in JavaScript and everything is an object.
Not true. There is no notion of classes in JavaScript implemented for HTML
user agents, and there are not only objects but also primitive values.
How many objects are created in the following code including the
objects implicitly created in the behind?

function C(){
}

function D(){
}
D.prototype = new C

var obj = new D
Four that are obvious. Two Function objects referred to by C and D each,
and two Object objects referred to by D.prototype and obj each, where D
objects (short for "objects created through the D constructor") are Object
objects that inherit from C.prototype.

window.alert([C, D, D.prototype, obj]);

Other implicitly created objects include the Global Object and all other
core objects. See ECMAScript 3 for details.[2]
[Q3]
I saw some codes like "this.base = SomeClass".
When is it needed?
When you want to (re)define a property of the object to be created, the
calling object or the Global Object; which one depends on the execution
context.
Is it different from ClassName.prototype = new ParentClassName?
It is completely different from that. The latter makes a ParentClassName
object the prototype object of ClassName objects, and so makes ClassName
objects inherit from ParentClassName.prototype.
[Q4]
What''s the difference between prototype and __proto__?



'prototype''是ECMAScript中为
$属性定义的标识符b $ b用于引用其原型对象的函数对象。


`__proto__''是一个JavaScript专有的标识符,用于

属性创建的所有对象`new''引用原型对象

的构造函数; ISTM表示`x .__ proto__''是

`x.constructor.prototype''的快捷方式[3],尽管

`x .__ proto__ == = x.constructor.prototype''等于'false''。

HTH


PointedEars

___________

[1]<网址:http://groups.google.com/group/comp.lang.javascript>

[2]<网址:http://www.mozilla .org / js / language />

[3]

< URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide: Details_of_the_Object_Mo del#Inheriting_Properties>



`prototype'' is an identifier defined in ECMAScript for properties of
Function objects used to refer to their prototype objects.

`__proto__'' is a JavaScript-proprietary identifier for properties of
all objects created through `new'' to refer to the prototype objects
of their constructors; ISTM that `x.__proto__'' is a shortcut for
`x.constructor.prototype'' as documented[3], even though
`x.__proto__ === x.constructor.prototype'' equals `false''.
HTH

PointedEars
___________
[1] <URL:http://groups.google.com/group/comp.lang.javascript>
[2] <URL:http://www.mozilla.org/js/language/>
[3]
<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Details_of_the_Object_Mo del#Inheriting_Properties>


Thomas,


感谢您的回答。

他们是非常令人印象深刻和乐于助人。


我还有一个问题。


托马斯''PointedEars''Lahn写道:
Thomas,

Thank you for the answers.
They are very impressive and helpful.

I have a further question.

Thomas ''PointedEars'' Lahn wrote:
Sam Kong写道:
Sam Kong wrote:
[Q1]
以下两个有什么区别?

(1)函数C(){
thi s.name =" Unknown"
}

(2)
函数C(){
}
C.prototype.name ="未知
[Q1]
What''s the difference between the following two?

(1)
function C(){
this.name = "Unknown"
}

(2)
function C(){
}
C.prototype.name = "Unknown"



使用(1),创建的对象拥有该属性。意味着通过相同的构造函数创建的对象可能仍然具有不同的属性
和属性值,如果它们之后被修改。

使用(2),创建的对象的原型具有属性,因此
对象通过原型链继承属性。这意味着如果修改了prototype属性/方法`name'',那么通过这个没有自己拥有`name'属性的构造函数创建的所有对象,
在某种意义上都有他们的`name''属性被修改为在这种情况下
原型链在属性访问时被访问。



With (1), the created object owns the property. Meaning that objects
created through the same constructor may still have different properties
and property values if they are modified afterwards.

With (2), the prototype of the created object has the property, so the
object inherits the property through the prototype chain. Meaning that
if you modify the prototype property/method `name'', all objects created
through this constructor that do not own a `name'' property themselves,
in a sense have their `name'' property modified as in that case the
prototype chain is accessed on property access.




请参阅以下代码。


函数C(){

this.name ="测试"

}


函数D (){

}

D.prototype = new C


var obj = new D

obj.name =" Test2" //这里,我有一个问题。


函数C(){

this.name ="测试"

}


函数D(){

}

D.prototype = new C


var obj = new D

obj.name =" Test2"


在最后一行,它是否为该物业添加了一个新槽或是吗?
修改现有房产''名称'的价值?


谢谢。


Sam



See the following code.

function C(){
this.name = "Test"
}

function D(){
}
D.prototype = new C

var obj = new D
obj.name = "Test2" //HERE, I HAVE A QUESTION.

function C(){
this.name = "Test"
}

function D(){
}
D.prototype = new C

var obj = new D
obj.name = "Test2"

At the last line, does it add a new slot for the property or does it
modify the value of existing property ''name''?

Thanks.

Sam


你好,


" Sam Kong"写道:
Hello,

"Sam Kong" wrote:

我最近对JavaScript的基于原型的面向对象很感兴趣。
但是,我仍然不清楚这个机制。

[Q1]
以下两个有什么区别?

(1)
功能C(){
this.name =" Unknown"
}

(2)
功能C(){
}
C.prototype.name =" ;未知


当你要实现一个物体时


foobar = new C();


与(1)属性name将被明确复制到实例


中,其中包含(2)属性name和将由代表团继承

意味着实例foobar没有属性name的副本

但是可以在原型链之后访问它


如果你创建了10个实例

wiht(1)你有10个不同的名字。属性

with(2)name属性由每个实例共享

[Q2]
JavaScript中没有类的概念,一切都是对象。


没有,但你有建设者的概念,可以模仿课程


cf ECMA-262 4.2.1

" ECMAScript不包含适当的课程,例如C ++,Smalltalk,

或Java,而是支持通过执行

代码来创建对象的构造函数,该代码为对象分配存储并通过为初始值分配初始值来初始化全部或部分

他们的财产。所有构造函数都是

对象,但并非所有对象都是构造函数。每个构造函数都有一个

Prototype属性,用于实现基于原型的继承和

共享属性。通过在新的

表达式中使用构造函数来创建对象;例如,new String(A String)创建一个新的String

对象。在不使用new的情况下调用构造函数会导致

依赖于构造函数。例如,String(A String)生成一个

原始字符串,而不是对象。


参见 http://www.ecma-international.org/pu...s/Ecma -262.htm

在以下代码中创建了多少个对象,包括隐藏在后面的
对象?

函数C() {
}

函数D(){
}
D.prototype = new C

var obj = new D

以上代码段


4如果您考虑创建原型属性为

not-an- object-instanciation


函数是对象,一旦你声明一个你创建一个对象

当你使用new时关键词你实例化一个对象


5如果你认为原型属性的创建是一个对象

instanciation

$ b后面的$ b函数C自动定义原型。物业

相当于

C.prototype = Function.prototype;


见ECMA-262 13.2创建功能对象

"

[...]

4.将F的[[Prototype]]属性设置为原始函数原型

对象,如第15.3.3.1节所述。

[...]

9.创建一个新对象,如表达式new

Object()。


10.将Result(9)的构造函数属性设置为F.给出此属性

属性{DontEnum}。


11.将F的prototype属性设置为Result(9)。这个属性给出了

属性,如第15.3.5.2节所述。

[...]

注意原型属性是自动创建的每个函数,

,以允许该函数用作

构造函数。

"






只需写完

函数C(){

}

DO创建

C.prototype = new Object();


[Q3]
我看到了一些像this.base = SomeClass这样的代码。
什么时候需要?
它与ClassName.prototype = new ParentClassName不同吗?


你能提供吗?一些代码构造?


我会尝试猜测...


通常当你想继承一个构造函数时>

你能做什么


功能A(){

}


功能B(){

}


B.prototype = new A();


但这样做会覆盖原始的B原型。属性

所以你也覆盖了构造函数包含在原型中的属性

foo = new A();

//foo.constructor == A


bar = new B();

//bar.constructor == A,而不是B


来纠正这种行为你可以重新分配

覆盖原型之后的构造函数


函数B(){

}


B.prototype = new A();

B.prototype.constructor = B;


所以现在当你用B实例化objet时构造函数


bar = new B();

//bar.constructor == B

for the this .base = SomeClass"。

我想你在那个环境中看到模仿superClass构造函数

调用


函数A( x){

this.x = x;

}


函数B(x,y){

this.base = A;

this.base(x);

this.y = y;

}


B.prototype = new A();
" this.base"允许在B

构造函数中执行构造函数

但是具有B函数范围,所以x允许执行构造函数。在B的背景下创建并且

不是A


或者你也可以这样做


函数B (x,y){

A.call(this,x); //相当于super(x)

this.y = y;

}


B.prototype = new A();


[Q4]
原型与__proto __有什么区别?

I got recently intrigued with JavaScript''s prototype-based
object-orientation.
However, I still don''t understand the mechanism clearly.

[Q1]
What''s the difference between the following two?

(1)
function C(){
this.name = "Unknown"
}

(2)
function C(){
}
C.prototype.name = "Unknown"

when you will instanciate an object

foobar = new C();

with (1) the property "name" will be explicitly copied into the instance

with (2) the property "name" will be inherited by delegation
meaning the instance "foobar" does not have a copy of the property "name"
but can access it following the prototype chain

if you create 10 instances
wiht (1) you have 10 different "name" properties
with (2) the "name" property is shared by every instances

[Q2]
There''s no notion of Class in JavaScript and everything is an object.
no but you have notion of constructors which can "emulate" classes

cf ECMA-262 4.2.1
"ECMAScript does not contain proper classes such as those in C++, Smalltalk,
or Java, but rather, supports constructors which create objects by executing
code that allocates storage for the objects and initialises all or part of
them by assigning initial values to their properties. All constructors are
objects, but not all objects are constructors. Each constructor has a
Prototype property that is used to implement prototype-based inheritance and
shared properties. Objects are created by using constructors in new
expressions; for example, new String("A String") creates a new String
object. Invoking a constructor without using new has consequences that
depend on the constructor. For example, String("A String") produces a
primitive string, not an object."

see http://www.ecma-international.org/pu...s/Ecma-262.htm
How many objects are created in the following code including the
objects implicitly created in the behind?

function C(){
}

function D(){
}
D.prototype = new C

var obj = new D

for the segment of code above

4 if you consider the creation of a prototype property as
not-an-object-instanciation

function are objects, once you declare one you create an object
when you use the "new" keyword you instanciate an object

5 if you consider that the creation of the prototype property is an object
instanciation

function C in the behind automatically define a "prototype" property
which is equivalent to
C.prototype = Function.prototype;

see ECMA-262 13.2 Creating Function Object
"
[...]
4. Set the [[Prototype]] property of F to the original Function prototype
object as specified in section 15.3.3.1.
[...]
9. Create a new object as would be constructed by the expression new
Object().

10. Set the constructor property of Result(9) to F. This property is given
attributes { DontEnum }.

11. Set the prototype property of F to Result(9). This property is given
attributes as specified in section 15.3.5.2.
[...]
NOTE A prototype property is automatically created for every function,
to allow for the possibility that the function will be used as a
constructor.
"

and


just writing
function C() {
}

DO create
C.prototype = new Object();

[Q3]
I saw some codes like "this.base = SomeClass".
When is it needed?
Is it different from ClassName.prototype = new ParentClassName?

could you provide some code constructs ?

I will try to "guess"...

usually when you want to inherit a constructor

you can do

function A(){
}

function B(){
}

B.prototype = new A();

but doing that you override the original B "prototype" property
and so you override also the "constructor" property contained in "prototype"

foo = new A();
//foo.constructor == A

bar = new B();
//bar.constructor == A, and not B

to correct this behaviour you can reassign the constructor property after
overriding the prototype

function B(){
}

B.prototype = new A();
B.prototype.constructor = B;

so now when you instanciate an objet with the B constructor function

bar = new B();
//bar.constructor == B
for the "this.base = SomeClass".
I suppose you seen that in that context to emulate superClass constructor
call

function A( x ){
this.x = x;
}

function B( x, y ){
this.base = A;
this.base( x );
this.y = y;
}

B.prototype = new A();

in this case "this.base" allow to execute A constructor inside the B
constructor
but with the B function scope, so the "x" is created in the context of B and
not A

alternatively you can also do that

function B( x, y ){
A.call( this, x ); // equivalent to "super( x )"
this.y = y;
}

B.prototype = new A();

[Q4]
What''s the difference between prototype and __proto__?




首先让'定义一个上下文


函数A(){

}


foobar =新的A();


在foobar的上下文中

foobar.prototype不存在

foobar.constructor是指向A构造函数的指针(因此名称)

foobar .__ proto__是指向A.prototype对象的指针


在A的上下文中;

A.prototype是一个实例化的对象

(参见上面的9。创建一个新的对象,将由表达式构建

新的Object()。")


这里我们用JSDB获得的东西( www.jsdb.org

----------------------------- ------- <无线电通信/>
函数A()

{


}

foobar = new A();


println(foobar.constructor === A); // true

println(foobar.prototype === undefined); // true

println(foobar .__ proto__ === A.prototype); // true

println(foobar.constructor.prototype === A.prototype); // true

println(" - ");

println((A.prototype).constructor === A); // true

println((A.prototype).prototype === undefined); // true

println((A.prototype).__ proto__ === Object.prototype); // true

println((A.prototype).constructor.prototype === A.prototype); // true

println(" - ");

println(A.constructor === Function); // true

println(A.prototype!== undefined); // true

println(A .__ proto__ === Function.prototype); // true

println(A.constructor.prototype === Function.prototype); // true

println(" - ");

println(foobar instanceof A); // true

println(一个instanceof函数); // true

println(A.prototype instanceof Object); // true

------------------------------------


有3个不同的对象


A是一个Function对象

A.prototype是一个Object对象

foobar是一个A对象


每个对象作为自己的构造函数属性指向

创建它的函数

每个对象都是自己的__proto__属性,指向创建它的

function.prototype

(除非你覆盖原型做什么,如A. prototype = new

B())


A是特例因为它是一个Function对象

所以原型属性会自动分配给他

默认情况下这个属性是一个简单的Object对象

$ b实现__proto__

的主机$ b你可以看到__proto__为可见或可访问的或可访问的构造函数原型


zwetan



first let''s define a context

function A(){
}

foobar = new A();

in the context of "foobar"
foobar.prototype does NOT exist
foobar.constructor is a pointer to A constructor function (hence the name)
foobar.__proto__ is a pointer to A.prototype object

in the context of "A"
A.prototype is an instancied object
(see above "9. Create a new object as would be constructed by the expression
new Object().")

here what we obtain with JSDB (www.jsdb.org)
------------------------------------
function A()
{

}
foobar = new A();

println( foobar.constructor === A ); //true
println( foobar.prototype === undefined ); //true
println( foobar.__proto__ === A.prototype ); //true
println( foobar.constructor.prototype === A.prototype ); //true
println( "--" );
println( (A.prototype).constructor === A ); //true
println( (A.prototype).prototype === undefined ); //true
println( (A.prototype).__proto__ === Object.prototype ); //true
println( (A.prototype).constructor.prototype === A.prototype ); //true
println( "--" );
println( A.constructor === Function ); //true
println( A.prototype !== undefined ); //true
println( A.__proto__ === Function.prototype ); //true
println( A.constructor.prototype === Function.prototype ); //true
println( "--" );
println( foobar instanceof A ); //true
println( A instanceof Function ); //true
println( A.prototype instanceof Object ); //true
------------------------------------

there are 3 different objects

A is a Function object
A.prototype is an Object object
foobar is an A object

each object as its own constructor property pointing to the function who
created it

each objects as its own __proto__ property pointing to the
function.prototype who created it
(except if you override the prototype doins something as, A.prototype = new
B() )

A is a "special case" because it''s a Function object
so a prototype property is automatically assigned him
by default this property is a simple Object object

for hosts implementing __proto__
you can see __proto__ as the "visible" or "accessible" constructor prototype

zwetan


这篇关于关于在自定义对象上创建属性的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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