What does the word "@" in "self=@" mean?

Asked 2 years ago, Updated 2 years ago, 89 views

I am reading the jQuery plug-in written in CoffeeScript, but I do not understand the description of @.What does it stand for?

self=@

Also, what does @ mean in Javascript and jQuery?
Even if I look for it in the book, there is no description of @.

javascript jquery coffeescript

2022-09-30 15:49

2 Answers

Description of the home site
All together now:
In the comparison table section of ,

CoffeeScript JavaScript
@, this this

It appears to represent this in JavaScript (you can think of it as the same).
As a shortcut for this.property, you can use@property.
There is an explanation that
this.property seems to be a shortcut to write like @property.


2022-09-30 15:49

JavaScript changes the meaning of this in the same context depending on where it is called.
In CoffeeScript, you can use @ in a cool way other than reducing the number of this types.

For example, if you want to do something like this

 hoge=function(){
  var foo;
  This.piko=123
  foo=function(){
    return this.piko
  }
}

The piko property declared above cannot be used in the foo function.
If you really want to use it, you will need a little trick.

 hoge=function(){
  varself = this;
  var foo;
  This.piko=123;
  foo=function(){
    return self.piko
  }
};

This is what happens when you write in CoffeeScript to achieve this.

hoge=->
  @piko=123
  foo==>
    @piko

If you want to bring this into the unknown function,
By simply defining a function in =>, you can generate a second roundabout JavaScript.
(I think a similar specification was adopted in ES6)


2022-09-30 15:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.