Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Setting "this"

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Setting "this"

    Right now I'm just playing around (not that I ever do any work anyways ) 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.

    Code:
    <html>
    <head>
    	<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);
    	</script>
    </head>
    <body>
    </body>
    </html>

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •