JavaScript Strange `bind` behavior strange bird behavior,strange bird behavior 2019,strange bird behavior

I'm trying to make a function which is similar to React's createClass function. It should take a POJO and turn it into constructor function which can be invoked with additional arguments.

Here's the code:

function createClass(obj) {
  return function(args = {}) {
    Object.keys(obj).forEach(k => this[k] = obj[k]);
    Object.keys(args).forEach(k => this[k] = args[k]);
    Object.keys(this.actions).forEach(k => this.actions[k] = this.actions[k].bind(this));
    console.log('binded to', this);
  }
}

const MyClass = createClass({
  actions: {
    foo: function() {
      console.log('actions.foo');
      this.request();
    }
  },
  foo: function() {
    console.log('foo');
    this.request();
  }
});

const req1 = function() {
  console.log('req1 called')
}

const req2 = function() {
  console.log('req2 called')
}

const c1 = new MyClass({request: req1});
const c2 = new MyClass({request: req2});

// As expected
c1.request();
c2.request();
console.log('---')

// As expected
c1.foo();
c2.foo();
console.log('---')

// Error: both call req1
c1.actions.foo();
c2.actions.foo();
console.log('---')

I don get why calling c2.foo() works as expected, but calling c2.actions.foo() instead calls method from another instance. How can it be possible?

Also, there is jsbin example

Answer:1

I'd like to replace javascript's built in alert() function with a React Component that handles the same task: namely, showing a quick, dismissible message to the user. Now, I can make this happen by ...

I'd like to replace javascript's built in alert() function with a React Component that handles the same task: namely, showing a quick, dismissible message to the user. Now, I can make this happen by ...

  1. react dynamically insert component

I have a React components. I want to test them. But I get unexpected error on every trying to findRenderedDOMComponentWithTag with tested component. Here is the Karma error log: 05 09 2015 20:31:23....

I have a React components. I want to test them. But I get unexpected error on every trying to findRenderedDOMComponentWithTag with tested component. Here is the Karma error log: 05 09 2015 20:31:23....

  1. reactjs state undefined in function
  2. reactjs undefined is not a function
  3. react js function undefined
  4. react state undefined in function

window.onerror = function(e){alert(e)}; function main(){ this.work = []; this.start_working = function() { try{ if(this.work.length > 0){ var y = this....

window.onerror = function(e){alert(e)}; function main(){ this.work = []; this.start_working = function() { try{ if(this.work.length > 0){ var y = this....

  1. problems with calling on iphone
  2. problems with calling 911
  3. problems with calling ios
  4. problems calling with o2
  5. problems with wifi calling
  6. problems with wifi calling on iphone 7
  7. problems with wifi calling iphone 6
  8. problems with cold calling
  9. problems with wifi calling iphone x
  10. problems with alexa calling
  11. problems with wifi calling on iphone 8
  12. problems with video calling
  13. problems with wifi calling on ee
  14. problems with wifi calling on iphone xr
  15. problems with wifi calling iphone
  16. problems with conference calling
  17. problems calling org.antlr.v4.gui.testrig.main(args)
  18. problems calling out iphone
  19. problems calling germany
  20. problems calling mexico

Which of these is normally seen as better when writing JavaScript. Foo, bar, and baz are specific to this function so won't be used anywhere else in other functions. function() { foo(); bar();...

Which of these is normally seen as better when writing JavaScript. Foo, bar, and baz are specific to this function so won't be used anywhere else in other functions. function() { foo(); bar();...