0

As the question says,

I want to know that if there a way to change scope of a function like this,

function foo(){
    var t = this;
    log(t);//{bar:'baz'}
    /*** Do something over here to change the scope ***/
    var newThis = this;
    log(newThis); //{something:'somethingelse'}
}

I am just curious to know, if there is a way.

Thanks

1 Answer 1

1
function foo(){
    log(this);
    /*** Do something over here to change the scope ***/
    with (newThis) {
        log(this);
    }
}

I wouldn't recommend using it though.

Using with is not recommended, and is forbidden in ECMAScript 5 strict mode. The recommended alternative is to assign the object whose properties you want to access to a temporary variable.

https://developer.mozilla.org/index.php?title=En/Core_JavaScript_1.5_Reference/Statements/With

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.