2

I'm working through some code a previous developer has written and have came across the following notation:

'.js-enter-new-address click'() {}

The code works but I dont understand why. Can anyone point me to any documentation of how this works? as i've not come across js written in this form before. I would usually expect it to be:

$('.js-enter-new-address').on('click', function(event) {}

Update I've noticed this code is part of the following, Please see below:

    const deliveryAddressComponent = Component.create('.checkout-delivery-address', { 
'.js-enter-new-address click'() {},
});
8
  • 1
    The first one is not valid syntax as you're calling a string as a function. You must be missing something. Commented Apr 9, 2020 at 11:42
  • 2
    Are you certain that's the syntax used? While it's technically possible to invoke the string constructor, it doesn't work like that, and even if it did following it with braces would be a syntax error. The only thing I can think is that there's some kind of transpiler converting that code to jQuery/JS. However I don't see the point doing all of that just to save 7 characters Commented Apr 9, 2020 at 11:43
  • You're right, I didn't mean to infer that's what this code does. Perhaps badly worded on my part Commented Apr 9, 2020 at 11:45
  • Thanks for the replys @RoryMcCrossan, I've updated the code to include an extra bit I've noticed Commented Apr 9, 2020 at 11:48
  • 2
    Which framework is being used? Angular, React and even C# ASP.Net has Component objects which can be created in similar ways to what you've shown Commented Apr 9, 2020 at 11:53

1 Answer 1

1

Its part of a javascript object that is send as a parameter of the create function of the Component object.

I don't know what this Component is or does since it's not native js.

Although the definition of the javascript object parameter seems weird it does seem to work as you can see in the snippet below.

var obj =  { 
'.js-enter-new-address click'() {},
}

console.log(obj);

Sign up to request clarification or add additional context in comments.

2 Comments

The syntax uses javascript's method in an object shorthand, where you can provide a name, list of parameters, and function body.
Thank you at least I usedstand why it works now, I will investigate further to see if I can find out what the component does. Thanks You everyone for your help.

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.