Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My opinion: Use of self is a bad idea #2

Closed
DamonOehlman opened this issue Jan 27, 2015 · 9 comments

Comments

@DamonOehlman
Copy link

commented Jan 27, 2015

For a couple of reasons:

  1. self resolves to the global window context in the same way that this does, well at least that's been my experience and have been confused by it in the past. Open the dev console in chrome and eval self for an example.
  2. It assumes that there is only one item that will have it's context dissociated on callback firing. While this is generally true when you are writing nice clean code, there are occasionally instances where this will not be true.

Like I said though, this is just my opinion...

@DamonOehlman

This comment has been minimized.

Copy link
Author

commented Jan 27, 2015

Just checked Firefox and self -> Window also...

@joshgillies

This comment has been minimized.

Copy link

commented Jan 27, 2015

So advocating the use of Function.prototype.bind() as opposed to var that = this or var self = this as a convention?

@DamonOehlman

This comment has been minimized.

Copy link
Author

commented Jan 27, 2015

I think var foo = this is a reasonable approach, and I definitely do it frequently. Usually though I replace self with something that is more meaningful with regards to object instance. While I'm not a fan of using that it doesn't fall victim to problem 1 so might be ok.

@feross

This comment has been minimized.

Copy link
Member

commented Jan 27, 2015

You are correct that self resolves to window in the browser. Here's an MDN page on the behavior: https://developer.mozilla.org/en-US/docs/Web/API/Window.self

This module catches the case where the user forgets to declare var self = this and accidentally uses the window.self.

I know no one who would ever intentionally use window.self instead of window, so the following error is given:

Error: Code style check failed:
  /Users/feross/code/standard/index.js:47:3: 'self' is not defined.

I think self is a lot clearer than that or _this. Your approach of naming based on the class is good too, but I still prefer self for consistency.

@feross

This comment has been minimized.

Copy link
Member

commented Jan 27, 2015

Fun fact: Did you know that self.window.self.window.self.self.self === window?

@feross

This comment has been minimized.

Copy link
Member

commented Jan 27, 2015

@DamonOehlman Given that there's a check for accidental window.self usage, would you say point 1 is sufficiently addressed?

@DamonOehlman

This comment has been minimized.

Copy link
Author

commented Jan 27, 2015

Yeah absolutely mate - and thanks for the fun fact :)

@tbranyen

This comment has been minimized.

Copy link

commented Mar 2, 2015

Can you use standard when writing WebWorker code where you cannot avoid the self global?

Workers run within a global context different from the current window. Thus, using the window shortcut to get the current global scope (instead of self) within a Worker will return an error.

@feross

This comment has been minimized.

Copy link
Member

commented Mar 4, 2015

Yep.

@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
4 participants
You can’t perform that action at this time.