4

Trying to figure out the syntax for doing something like this using ES6 classes:

function Component() {

}

Component.prototype.render = style(function() {

});

This is what I have so far:

class Component {

  constructor() {
    super()
  } 

  render style(() {

  });
}

Any ideas?

2 Answers 2

6

Since class is just syntactic sugar, this should work:

class Component {
    constructor() {
        super()
    } 
}

Component.prototype.render = style(function() {
});
Sign up to request clarification or add additional context in comments.

Comments

2

here is what torazaburo already showed you, http://www.es6fiddle.net/i67l1pjr/.

var style=function(fun){
  // do something with fun

  return fun
}

class Component {
    constructor() {
        super()
    }       
}

Component.prototype.render = style(function() {
  console.log('i am invoked');
});


var x=new Component;
x.render();

I assume your style function is just like the one i have defined, now in this case, you can easily acheive the desired result(creating a function returned by another function) using the old way of defining a method.

USing ES-6 syntax

Now as we know es6 classess are just syntactic sugars , and we must be able to acheive everything using classes, as we did before.

see this. http://www.es6fiddle.net/i67l1d4e/

var style=function(fun){
  // do something with fun

  return fun
}

class Component {
    constructor() {
        super()
    } 


  render (){    // this is the new render function, you wanted to define

    var fun =style(function() {
      console.log('i am invoked');
       });

    return new fun();
  }

}

var x=new Component;
x.render();

Now both of these ways, do the same thing.. its just a different syntax added by ES-6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.