Class decorator with stage-2 proposal and Babel 7Are static class variables possible?How to change an element's class with JavaScript?Decorator classes in PythonHow to make a chain of function decorators?How can I select an element with multiple classes in jQuery?Python class inherits objectCreating a singleton in PythonHow does the @property decorator work?Access a class attribute inside a python class decoratordecorating a class function with a callable instance
FindInstance and cosine system of equations
How to improve/restore vintage Peugeot bike, or is it even worth it?
What is the unit of the area when geometry attributes are calculated in QGIS?
What happens to matryoshka Mordenkainen's Magnificent Mansions?
Are we obligated to aspire to be Talmidei Chachamim?
What are the spoon bit of a spoon and fork bit of a fork called?
How can I support myself financially as a 17 year old with a loan?
What happens if I start too many background jobs?
How to generate a customised histogram with pgf plots?
What are the differences between credential stuffing and password spraying?
Why wasn't the Night King naked in S08E03?
Would "lab meat" be able to feed a much larger global population
How would adding a darkvision racial trait to Dragonborn affect balance?
Was there ever a Kickstart that took advantage of 68020+ instructions that would work on an A2000?
For a benzene shown in a skeletal structure, what does a substituent to the center of the ring mean?
What word means "to make something obsolete"?
Airbnb - host wants to reduce rooms, can we get refund?
How to explain the behaviour of TreeForm?
Is Cola "probably the best-known" Latin word in the world? If not, which might it be?
Is one octave above tonic also considered as tonic?
In a vacuum triode, what prevents the grid from acting as another anode?
How did Arya get her dagger back from Sansa?
Returning the outputs of a nested structure
Is a lifestealing melee cantrip, in the form of booming blade, unbalanced?
Class decorator with stage-2 proposal and Babel 7
Are static class variables possible?How to change an element's class with JavaScript?Decorator classes in PythonHow to make a chain of function decorators?How can I select an element with multiple classes in jQuery?Python class inherits objectCreating a singleton in PythonHow does the @property decorator work?Access a class attribute inside a python class decoratordecorating a class function with a callable instance
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am playing with stage-2 decorators proposal and I have found some problems with class decorators. In legacy decorator, the next example works because class decorators took as their only argument a target constructor, but with stage-2 proposal receives a object descriptor.
function log(Class)
return (...args) =>
console.log(`Arguments: $args`);
return new Class(...args);
;
@log
class Bar
constructor(name, age)
I have read the tc39 proposal-decorators and built-in decorators,but it did not help me so much. How can I make this example works with the new proposal decorator? is it possible to use built-in decorators with babel?
To try some stuff, I developed my own basic wrap decorator, it works as expected for methods, and I want to extend its functionality to class.
const wrap = f =>
return wrapped =>
const kind = wrapped;
if (kind === 'class')
return f(wrapped);
const descriptor = wrapped;
const original = descriptor.value;
descriptor.value = f(original);
return ...wrapped, descriptor ;
;
;
export default wrap;
With this basic decorator, I can create for example this logger method decorator that works.
import wrap from './wrap';
const loggerFunction = f =>
const name = f.name;
return function test(...args)
console.log(`starting $name with arguments $args.join(', ')`);
return f.call(this, ...args);
;
;
export default wrap(loggerFunction);
And I can use it by this way:
class Foo
@logger
method(argument)
return argument;
javascript class decorator babel ecmascript-next
add a comment |
I am playing with stage-2 decorators proposal and I have found some problems with class decorators. In legacy decorator, the next example works because class decorators took as their only argument a target constructor, but with stage-2 proposal receives a object descriptor.
function log(Class)
return (...args) =>
console.log(`Arguments: $args`);
return new Class(...args);
;
@log
class Bar
constructor(name, age)
I have read the tc39 proposal-decorators and built-in decorators,but it did not help me so much. How can I make this example works with the new proposal decorator? is it possible to use built-in decorators with babel?
To try some stuff, I developed my own basic wrap decorator, it works as expected for methods, and I want to extend its functionality to class.
const wrap = f =>
return wrapped =>
const kind = wrapped;
if (kind === 'class')
return f(wrapped);
const descriptor = wrapped;
const original = descriptor.value;
descriptor.value = f(original);
return ...wrapped, descriptor ;
;
;
export default wrap;
With this basic decorator, I can create for example this logger method decorator that works.
import wrap from './wrap';
const loggerFunction = f =>
const name = f.name;
return function test(...args)
console.log(`starting $name with arguments $args.join(', ')`);
return f.call(this, ...args);
;
;
export default wrap(loggerFunction);
And I can use it by this way:
class Foo
@logger
method(argument)
return argument;
javascript class decorator babel ecmascript-next
"I developed my own basic wrap decorator" - can you show how you're using that, and where it works (or not)?
– Bergi
Mar 23 at 12:50
@Bergi of course! I have edited my question with that example.
– Fco. Alejandro Jurado Pérez
Mar 23 at 13:06
add a comment |
I am playing with stage-2 decorators proposal and I have found some problems with class decorators. In legacy decorator, the next example works because class decorators took as their only argument a target constructor, but with stage-2 proposal receives a object descriptor.
function log(Class)
return (...args) =>
console.log(`Arguments: $args`);
return new Class(...args);
;
@log
class Bar
constructor(name, age)
I have read the tc39 proposal-decorators and built-in decorators,but it did not help me so much. How can I make this example works with the new proposal decorator? is it possible to use built-in decorators with babel?
To try some stuff, I developed my own basic wrap decorator, it works as expected for methods, and I want to extend its functionality to class.
const wrap = f =>
return wrapped =>
const kind = wrapped;
if (kind === 'class')
return f(wrapped);
const descriptor = wrapped;
const original = descriptor.value;
descriptor.value = f(original);
return ...wrapped, descriptor ;
;
;
export default wrap;
With this basic decorator, I can create for example this logger method decorator that works.
import wrap from './wrap';
const loggerFunction = f =>
const name = f.name;
return function test(...args)
console.log(`starting $name with arguments $args.join(', ')`);
return f.call(this, ...args);
;
;
export default wrap(loggerFunction);
And I can use it by this way:
class Foo
@logger
method(argument)
return argument;
javascript class decorator babel ecmascript-next
I am playing with stage-2 decorators proposal and I have found some problems with class decorators. In legacy decorator, the next example works because class decorators took as their only argument a target constructor, but with stage-2 proposal receives a object descriptor.
function log(Class)
return (...args) =>
console.log(`Arguments: $args`);
return new Class(...args);
;
@log
class Bar
constructor(name, age)
I have read the tc39 proposal-decorators and built-in decorators,but it did not help me so much. How can I make this example works with the new proposal decorator? is it possible to use built-in decorators with babel?
To try some stuff, I developed my own basic wrap decorator, it works as expected for methods, and I want to extend its functionality to class.
const wrap = f =>
return wrapped =>
const kind = wrapped;
if (kind === 'class')
return f(wrapped);
const descriptor = wrapped;
const original = descriptor.value;
descriptor.value = f(original);
return ...wrapped, descriptor ;
;
;
export default wrap;
With this basic decorator, I can create for example this logger method decorator that works.
import wrap from './wrap';
const loggerFunction = f =>
const name = f.name;
return function test(...args)
console.log(`starting $name with arguments $args.join(', ')`);
return f.call(this, ...args);
;
;
export default wrap(loggerFunction);
And I can use it by this way:
class Foo
@logger
method(argument)
return argument;
javascript class decorator babel ecmascript-next
javascript class decorator babel ecmascript-next
edited Mar 23 at 13:04
Fco. Alejandro Jurado Pérez
asked Mar 22 at 21:04
Fco. Alejandro Jurado PérezFco. Alejandro Jurado Pérez
7029
7029
"I developed my own basic wrap decorator" - can you show how you're using that, and where it works (or not)?
– Bergi
Mar 23 at 12:50
@Bergi of course! I have edited my question with that example.
– Fco. Alejandro Jurado Pérez
Mar 23 at 13:06
add a comment |
"I developed my own basic wrap decorator" - can you show how you're using that, and where it works (or not)?
– Bergi
Mar 23 at 12:50
@Bergi of course! I have edited my question with that example.
– Fco. Alejandro Jurado Pérez
Mar 23 at 13:06
"I developed my own basic wrap decorator" - can you show how you're using that, and where it works (or not)?
– Bergi
Mar 23 at 12:50
"I developed my own basic wrap decorator" - can you show how you're using that, and where it works (or not)?
– Bergi
Mar 23 at 12:50
@Bergi of course! I have edited my question with that example.
– Fco. Alejandro Jurado Pérez
Mar 23 at 13:06
@Bergi of course! I have edited my question with that example.
– Fco. Alejandro Jurado Pérez
Mar 23 at 13:06
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55307753%2fclass-decorator-with-stage-2-proposal-and-babel-7%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55307753%2fclass-decorator-with-stage-2-proposal-and-babel-7%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
"I developed my own basic wrap decorator" - can you show how you're using that, and where it works (or not)?
– Bergi
Mar 23 at 12:50
@Bergi of course! I have edited my question with that example.
– Fco. Alejandro Jurado Pérez
Mar 23 at 13:06