What are the differences between self in ruby โ€‹โ€‹and 'this' in javascript

I am familiar with ruby โ€‹โ€‹and start learning javascript. Is it safe to skip to find out 'this' and consider it the complete equivalent of a ruby โ€‹โ€‹self in javascript, and apply in javascript the same as instance_eval in ruby?

If not, what are the big differences between them?

+5
source share
2 answers

self widely used in Ruby Metaprogramming .

From Ruby Metaprogramming :

Each line of Ruby code is executed inside an object - the so-called current object. The current object is also known as โ€œme,โ€ because you can access it with the self keyword.

Only one object can take on the role of itself at a given time, but no object holds this role for a long time. In particular, when you call a method, the receiver becomes itself. From now on, all instances of the variables are instance variables of self, and all methods called without an explicit receiver are invoked on themselves. As soon as your code explicitly calls the method on some other object, that other object becomes itself.

For instance:

 class Foo attr_reader :name def initialize(name) @name = name end # here self indicates the class itself Foo def self.bar end # here self indicates the instance of class Foo def baz self.name end end 

Now:

 foo = Foo.new('FooBar') foo.baz #=> 'FooBar' 

While in JavaScript using this keyword is powerful and unique, it does not apply to an object (like ES5 strict mode) like Ruby, as shown above. this can be of any value. The value of this within any given function call is determined by how the function is called (not where the function is defined, i.e. Context, as in languages โ€‹โ€‹like Ruby, C # or Java). But, like Ruby self , this cannot be set by destination at runtime. For instance:

Global context ( directly copied here ):

 console.log(this.document === document); // true // In web browsers, the window object is also the global object: console.log(this === window); // true this.a = 37; console.log(window.a); // 37 

or as an object method:

 var foo = {}; foo.bar = function() { console.log(this.firstName); }; foo.firstName = 'FooBar'; foo.bar(); // "FooBar" 

From the above examples, it is clear that JavaScript this not exactly the cousin of Ruby self , but they have a little pinch of similarity in their behavior.

To learn Ruby self read this further and for JavaScript this read this .

+3
source

The JavaScript value of 'this' can be changed to everything, by default 'this' refers to the immediate parent. (therefore, a function in the global scope will refer to a window object.)

This is an arbitrary example, but should show the idea well enough. When you call a function like myFunc (), this is a shortcut to the expression myFunc.call (). The first argument that .call () takes is the value for 'this'.

Look again to call: https://developer.mozilla.org/enUS/docs/Web/JavaScript/Reference/Global_Objects/Function/call

 function foo() { console.log(this); } foo(); // this will log out the window object var bar = { fname: 'Edy', sayHello: function() { alert('Hello ' + this.fname); }, baz: function() { console.log(this); } } bar.baz(); // will log out the 'bar' object var person = { fname: 'Danny' } var person2 = { fname: 'Elenore' } function sayHello() { alert('Hello ' + this.fname); } sayHello.call(person); // will alert 'Danny' sayHello.call(person2); // will alert 'Elenore' 
0
source

Source: https://habr.com/ru/post/1206096/


All Articles