Re: Object oriented stuff and browsers related thing
- From: Thomas 'PointedEars' Lahn <PointedEars@xxxxxx>
- Date: Sat, 19 Nov 2005 21:46:35 +0100
Luke Matuszewski wrote:
> Here are some questions that i am interested about and wanted to here
> an explanation/discussion:
> 1. (general) Is the objectness in JavaScript was supported from the
> very first version of it (in browsers) ?
Yes, with Netscape 2.0 being the first (and only) browser to support
JavaScript 1.0. It is also supported since JScript 1.0 and specified
since ECMAScript Edition 1.
JavaScript < 2/JScript < 7/ECMAScript are object-oriented programming
languages using prototype-based inheritance.
JavaScript 2/JScript 7 (.NET)/ECMAScript 4 are object-oriented programming
languages supporting both prototype-based and class-based inheritance.
(However, there are only proposals and test implementations of those for
the first and the last yet.)
> What about the new syntax of
> creating a object using { 'propName1':'propValue1',
> 'propName2':'propValue2', 'propName3':{ /* another object */ } } - from
> what version of JScript/JavaScript it was supported (from what browsers
> versions) ?
Documentation (see e.g. <URL:http://jibbering.com/faq/>,
<URL:http://mozilla.org/js/language/>,
<URL:http://developer.mozilla.org/javascript/>
and
<URL:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/jscript7/html/jslrfjscriptlanguagereference.asp>)
states that such Object (object) literals are supported from
JavaScript version 1.3 (NN 4.06, Mozilla/5.0),
JScript version 3.0 (IE/Win 4+, IIS 4+),
ECMAScript Edition 3
on. The same goes for Array (object) literals: [..., ...].
Now that you mentioned it: Since I asked such questions myself often and
finally did not want to bother checking all references every time, I have
begun to create a support matrix on my local Web server and now have
uploaded the latest version of it to be accessible at
<http://www.PointedEars.de/scripts/js-version-info>
I intend to correct/complete it with the help of further tests,
documentation and posters of this group. (If the scrolling table
is garbled in Firefox, just increase text size via the View menu
or Ctrl++ and decrease it again (with Ctrl+-). That is a Firefox
bug I was willing to accept for local use.)
> 2. (object-oriented) Every object has a prototype (and so on the
> toString(), (other)... methods inherited from it). When i do sth
> like this:
>
> function MyConstr() {
> var thisVar = this; /* Protected pattern */
> var someProtected = 3; /* Protected pattern */
> var this.thisVar = this; /* Public pattern */
^^^^^^^^^
This (*g*) is a syntax error. It should be
this.thisVar = this; /* Public pattern */
> /* here 'this' specifies the reference to the created object via 'new'
> keyword */
True, iff MyConstr() is called as a constructor (with the NewExpression).
> this.myFunc = function () { /* Privilaged pattern */
> /* here 'this' specifies the reference to ... what ?
> 1. to function myFunc context - so it points no more to the newly
> created object via new MyConstr()
> (thus someone could say it is buggy and should use thisVar)
> 2. to newly created object via new MyConstr();
> */
2., unless Function.prototype.call() is used. But if you referred to `this'
as within the method definition, that would not be 2. because it is within
the constructor, but because the method is that of what will be the calling
object.
> }
>
> }
>
> MyConstr.myFunc2 = function () { /* Public pattern */
> /* here 'this' specifies the reference to ... what ?
> 1. to function myFunc2 context - so it points no more to the newly
> created object via new MyConstr()
> (thus someone could say it is buggy and should use this.thisVar)
> 2. to newly created object via new MyConstr();
> */
> }
Neither one. It refers to `MyConstr' as that would be the
calling object, unless Function.prototype.call() is used.
> MyConstr.prototype.myFunc3 = function () {
> /* here 'this' references to what object/contex ?
> */
> }
`this' refers to the object that `MyConstr' is the constructor of or that
is derived from `MyConstr', unless Function.prototype.call() is used. So
it refers to the calling object again.
> Questions:
> 2.1 What is the difference between the myFunc and myFunc2 ? I know that
> inserting function inside constructor takes more memory when
> instantiating new objects than outside like myFunc2... but myFunc has
> acces to someProtected variable and myFunc2 does not. Can anyone point
> other differences ?
I think we have clarified this about 15 minutes before your posting
in <URL:news:1132336168.099045.87510@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>.
Since it was posted via Google Groups, it should already be archived
there.
> 2.2 What about the issues with 'this' keyword ? (see more comments
> about it in source code provided above - especially concerning about
> what object/contex 'this' references)
It refers to the calling or created object or to the Global Object
if there is none of the former.
> 2.3 Properties added outside the constructor body are accessible
> without first creating an object right ? so one could say they are
> static... is this correct ? (any other issues).
Correct.
> 2.4 Any other suggestions on OOP in JS ? please write sth or provide a
> link (i have read faq_notes on jiberring).
For example <URL:http://crockford.com/javascript/javascript.html> pp.
> 3. I have heard that statement
> MyConstr.myFunc2 = function ()
> was bad supported in IE5 ? (or maybe a new Function(...) construction).
> Please provied a link/comment ?
The function operator (first line) is documented to be supported from
JavaScript version 1.5 (see above; however it works in NN 4.8 as well)
JScript ?
ECMAScript Edition 3
on. The Function constructor (second line) is documented to be supported
from
JavaScript version 1.1 (NN 3.0, NES 2.0)
JScript version 2.0 (IE/Win 3, IIS 3)
ECMAScript Edition 1
on.
HTH
PointedEars
.
- Prev by Date: Re: Some interesing aspect of injecting scripts on page...
- Next by Date: Re: Some interesing aspect of injecting scripts on page...
- Previous by thread: Re: Object oriented stuff and browsers related thing
- Next by thread: Re: Object oriented stuff and browsers related thing
- Index(es):
Relevant Pages
|