G
G
Gabe Jonson2016-08-08 14:50:05
JavaScript
Gabe Jonson, 2016-08-08 14:50:05

Javascript objects?

Guys, a question in the studio! ;)

var test = {
  getText: function(some) {

  },
  otherSome: function(someOther) {
    this.getText(); // Не работает
    test.getText(); // Работает
  }
}

Why is that?
PS. And how to implement such a thing ...? The one that doesn't work...

Answer the question

In order to leave comments, you need to log in

4 answer(s)
Y
yociyavi, 2016-08-08
@gabejonson

Everything is working.
plnkr.co/edit/T34u0iak4WurB9zdjcq6?p=preview

D
Dmitry Belyaev, 2016-08-08
@bingo347

Why is that?
this refers to the context in which the function was called
test.otherSome(); //контекст - test
test.otherSome.call(null); //контекст - null
new test.otherSome(); //контекст - новый объект унаследованный от test.otherSome.prototype
(0, test.otherSome)(); //контекст - undefined (в strict mode) или глобальный объект (не в strict mode)
({ otherSome: test.otherSome }).otherSome(); //контекст - объект который в скобочках

See the examples above, all except the first one will throw an error that either getText cannot be read from null/undefined (2, 3 examples) or that getText is not a function (3, 5 examples)

E
Eugene, 2016-08-08
@Nc_Soft

this is the function inside which this is called

D
Denis Ineshin, 2016-08-08
@IonDen

It will be useful to read about it:
https://learn.javascript.ru/objects-more

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question