View Full Version : Setting "this"

07-30-2002, 03:07 PM
Right now I'm just playing around (not that I ever do any work anyways :D) with object prototypes. With the predefined objects you can change the value of "this" (so that the value of the object would be the value of this). I've had this error [invalid assignment left-hand side, line 8, char 7] before when I did this, and was just wondering if there was a way to work around it. Code is below.

<title>Setting "this"</title>
<script type="text/javascript">
function baseObject() {
baseObject.prototype.setThis=function(x) {
this=x; //line 8
var base=new baseObject();
base.setThis("hi!"); alert(base);

07-30-2002, 07:50 PM
To quote the ECMA-262 3rd edition spec (page 39, section 10.1.7):

"The this value associated with an execution context is immutable."

Immutable is fancy term often used in Python meaning something can't be changed in place, or is constant.