function Test(){ //... } Test.prototype = { data: undefined, working: null, // массив с инстенсами collectData: function(){ this.data = 'data'; // здесь я собираю данные }, run: function(){ // здесь прохожу по массиву инстенсов var length = this.working.length, item = undefined; for(var i = 0; i < length; i++){ item = this.working[i]; item.one.call(this); // и передаю им this в качестве контекста } } } /** * И все это сделано для того, * чтобы в объектах ниже я мог обращаться * к свойству data инстенса var test = new Test(); * как к своему... * * Делают так в js? Под "так" я подразумеваю * вызов методов из массива в инстенсе test * с передачей в эти методы себя в качестве * контекста? * И если так в js делают, то как это с точки * зрения ООП? */ var Obj1 = { one: function(){ console.log('obj1', this.data); }, two: function(){ //... } } var Obj2 = { one: function(){ console.log('obj2', this.data); }, two: function(){ //... } } var test = new Test(); test.working([new obj1(), new obj2()]); test.collectData(); test.run(); 

I have a Test object that is designed to process data that it receives by reference. It receives an unformatted string that needs to be very richly formatted, and for this I still have objects, in my case obj1 and obj2, each of which performs the necessary actions with the data.
And the question is - is it possible to do this in js and is this approach beyond the scope of the PLO?

  • It seems to me that, at first glance, it’s crooked, in theory, you should have an array of instances of the same type at working, for which you call item.one (), and if you need access to the entire collection, then pass this or working / data parameters. Otherwise it is somehow tangled. As if obj1, obj2 is a collection of utilities of some kind. - zb '10
  • Few corrected and supplemented. And the question was re-set in the comments. - vas
  • Well, I repeat, you have obj1, obj2 is not prototyped, i.e. he is just a collection of some utilities. They do both this and that, but it seems to me that we need to specify the task more, because it is not clear why this is required. This is usually done when events are processed, but in this case the callback is passed to the handler, the handler about the second object should not know anything in the hardcode. It all depends on the relationship between these entities. By doing so, you lose the opportunity to refer to obj1.two () from obj1.one (), i.e. obj1 turns into a namespace or collection of static methods. - zb '
  • Yes, objekty, in fact, an associative array or HashMap ... Not having their own properties, which is accessed. As some parasites obtained. Use. To be honest, I just asked :) Every time I do something with a context, I want to ask this question, because this method allows you to create object classes with more performance than the same extends method. - vas
  • My advice is to read good sources that are close to your theme. - zb '

0