> As written previously, I'm currently writing a master thesis on
> JavaScript - hence running into weird stuff in JS. I'm sure the task
[quoted text clipped - 3 lines]
>
> --- SNIP Start
Well I guessed 'efinfi', but when I ran the script, I was wrong. I
miscounted on the substring() method.
> As written previously, I'm currently writing a master thesis on
> JavaScript - hence running into weird stuff in JS. I'm sure the task
[quoted text clipped - 7 lines]
> {
> var x = 42;
That creates a local variable for the X Function object, it isn't
inherrited by objects created by new X().
x is assigned a value of 42, a number primitive.
> x.y = "hello world";
You can't assign to properties of a primitive or a Number object, for
that you must use Number.prototype. This statement does nothing.
> return x;
When X is called as a constructor with the new operator, the value
returned is a reference to the new object. If new isn't used, X will
return x.
> }
>
> var y = new X();
Since no values were assinged to properties of X's this keyword, none
were added to the new object. y has the same properties as
X.prototype, which is the same as Object.prototype (see ECMAScript
Language Specification section 15.2.4).
> var str = new String(y.y);
y.y is undefined, so the value of str is 'undefined'.
> print(str.substring(4,7)+str.substring(5,6)); // <-- Print
Better to use:
document.write(...);
substring(start, end) will include the character at start up to and
including the character immediately before end. If end is omitted, it
is the end of the string. Note that indexs are zero-based, the first
character is substring(0,1). The rest is in the ECMAScript Language
Specification section 15.5.4.15.
shows 'fini': the 4th to 6th characters inclusive concatenated with
the 5th character of 'undefined'.
--
Rob
> As written previously, I'm currently writing a master thesis on
> JavaScript - hence running into weird stuff in JS. I'm sure the task
[quoted text clipped - 7 lines]
> {
> var x = 42;
That creates a local variable for the X Function object, it isn't
inherrited by objects created by new X().
x is assigned a value of 42, a number primitive.
> x.y = "hello world";
You can't assign to properties of a primitive or a Number object, for
that you must use Number.prototype. This statement does nothing.
> return x;
When X is called as a constructor with the new operator, the value
returned is a reference to the new object. If new isn't used, X will
return x.
> }
>
> var y = new X();
Since no values were assinged to properties of X's this keyword, none
were added to the new object. y has the same properties as
X.prototype, which is the same as Object.prototype (see ECMAScript
Language Specification section 15.2.4).
> var str = new String(y.y);
y.y is undefined, so the value of str is 'undefined'.
> print(str.substring(4,7)+str.substring(5,6)); // <-- Print
Better to use:
document.write(...);
substring(start, end) will include the character at start up to and
including the character immediately before end. If end is omitted, it
is the end of the string. Note that indexs are zero-based, the first
character is substring(0,1). The rest is in the ECMAScript Language
Specification section 15.5.4.15.
shows 'fini': the 4th to 6th characters inclusive concatenated with
the 5th character of 'undefined'.
--
Rob
Rasmus Kromann-Larsen - 31 May 2007 16:09 GMT
> > As written previously, I'm currently writing a master thesis on
> > JavaScript - hence running into weird stuff in JS. I'm sure the task
> > will be easy for most regulars, but it's slightly tricky nontheless.
>
> > What does the following snippet print?
Correct - and impressive Rob :-)
> > --- SNIP Start
>
[quoted text clipped - 11 lines]
> You can't assign to properties of a primitive or a Number object, for
> that you must use Number.prototype. This statement does nothing.
Really? I believed that what this statement would do was to take the
primitive number, wrap it as a Number object, add the property y with
the value "hello world", then instantly discard the property as the
wrapper was destroyed. I'm pretty sure this is what happens.
> > return x;
>
> When X is called as a constructor with the new operator, the value
> returned is a reference to the new object. If new isn't used, X will
> return x.
Not entirely correct. The specification of ECMAScript (13.2.2 or
something? can't check it here) actually states that if you return
from a constructor, the object that you return will override the
object created by the constructor, which will be discarded. However,
it also says that if the value returned is not an object, it will
return the created object. But since x is a number and thus not an
object, the object created will be returned.
The rest of your observations are completely accurate :-)
- Rasmus.