destructuring

ES6 destructuring function parameter - naming root object

不打扰是莪最后的温柔 提交于 2020-01-09 11:39:11
问题 Is there a way to retain the name of a destructured function argument? I.e., the name of the root object? In ES5, I might do this (using inheritance as a metaphor to make the point): // ES5: var setupParentClass5 = function(options) { textEditor.setup(options.rows, options.columns); }; var setupChildClass5 = function(options) { rangeSlider.setup(options.minVal, options.maxVal); setupParentClass5(options); // <= we pass the options object UP }; I'm using the same options object to hold

ES6/ES2015 object destructuring and changing target variable

本秂侑毒 提交于 2020-01-08 21:43:08
问题 How can I rename the target during object destructing? const b = 6; const test = { a: 1, b: 2 }; const {a, b as c} = test; // <-- `as` does not seem to be valid in ES6/ES2015 // a === 1 // b === 6 // c === 2 回答1: You can assign new variable names, like shown in this MDN Example var o = { p: 42, q: true }; // Assign new variable names var { p: foo, q: bar } = o; console.log(foo); // 42 console.log(bar); // true So, in your case, the code will be like this const b = 6; const test = { a: 1, b: 2

Destructuring argument in Clojure

感情迁移 提交于 2020-01-06 08:44:30
问题 Newbie enjoying Clojure a lot here. So, I have an HTTP route: (POST "/login" request (post-login request)) here, "request" is a map with a lot of http things inside. And the "post-login" function: (defn post-login ;; Login into the app [{{email "email" password "password"} :form-params session :session :as req}] (let [user (modusers/get-user-by-email-and-password email password)] ;; If authenticated (if-not (nil? (:user user)) (do (assoc (response/found "/") :session (assoc session :identity

Object destructuring ({ x, y, …rest }) for whitelisting properties of an object [duplicate]

别来无恙 提交于 2020-01-03 17:19:07
问题 This question already has answers here : One-liner to take some properties from object in ES 6 (11 answers) Closed 3 years ago . Using Object rest destructuring is straightforward to blacklist properties of an object, like in the following example: const original = { a: 1, b: 2, c: 3, evil: "evil", ugly: "ugly", }; const { evil, ugly, ...sanitized } = original; console.log(sanitized); // prints { a: 1, b: 2, c: 3 } I wonder if there exists a similar terse way to do the same, but using a white

Avoid an error when destructuring from undefined

不问归期 提交于 2020-01-02 02:01:09
问题 Lets say I have this code: const {x, y} = point; Babel will turn this into: var _point = point, x = _point.x, y = _point.y; Which is fine, but what if point is undefined? Now I get an error: "Cannot read property 'x' of undefined" . So how do I avoid this? I want to do something like const {x, y} = {} = point; but that's a syntax error. I can only see that this is an option: const {x, y} = point || {}; Which babel transpiles to: var _ref = point || {}, x = _ref.x, y = _ref.y; Here we're

How to bind methods when destructuring an object in JavaScript?

て烟熏妆下的殇ゞ 提交于 2019-12-30 09:13:38
问题 How to bind methods when destructuring an object in JavaScript? const person = { getName: function() { console.log(this); } }; var a = person.getName; var b = person.getName.bind(person); var {getName: c} = person; person.getName(); //=> {getName: [Function]} a(); //=> window or global b(); //=> {getName: [Function]} c(); //=> window or global I want c to log in the console its "parent" object {getName: [Function]} . Is there any way to bind all methods when destructuring an object in one

How to bind methods when destructuring an object in JavaScript?

左心房为你撑大大i 提交于 2019-12-30 09:10:03
问题 How to bind methods when destructuring an object in JavaScript? const person = { getName: function() { console.log(this); } }; var a = person.getName; var b = person.getName.bind(person); var {getName: c} = person; person.getName(); //=> {getName: [Function]} a(); //=> window or global b(); //=> {getName: [Function]} c(); //=> window or global I want c to log in the console its "parent" object {getName: [Function]} . Is there any way to bind all methods when destructuring an object in one

ES6 Structuring Assignment?

跟風遠走 提交于 2019-12-30 01:01:06
问题 The new destructuring assignment features of ES6 are fairly well known now (live copy on Babel's REPL); in the case of variables that already exist: let a, b; // Existing variables let o = {a: "a", b: "b"}; // An object to get values from // ... ({a, b} = o); // Set them to the props from `o` console.log(a); // "a" console.log(b); // "b" Is there a simple converse in ES6? Setting properties on an existing object based on variables with the same name? (Other than the obvious o.a = a; o.b = b;

Constructor Destructuring with exposed parameters

☆樱花仙子☆ 提交于 2019-12-29 08:25:09
问题 I have a constructor which uses destrucuring to simplify what needs to be passed to create the object with the proper defaults. export class PageConfig { constructor({ isSliding = false }: { isSliding?: boolean; getList: (pagingInfo: PagingInfo) => Observable<any>; }) {} } I want to expose the properties passed into the constructor as public, preferable without redeclaring them. Additionally I would not like to have a middle man object. e.g say I had a class which instanciated my object like

Array destructuring in JavaScript

徘徊边缘 提交于 2019-12-28 03:03:31
问题 I have this code in my vue-js app: methods: { onSubmit() { ApiService.post('auth/sign_in', { email: this.email, password: this.password, }) .then((res) => { saveHeaderToCookie(res.headers); this.$router.push({ name: 'about' }); }) .catch((res) => { this.message = res.response.data.errors[0]; this.msgStatus = true; this.msgType = 'error'; }); }, } While running Es-lint.. I got an error saying "Use array destructuring" (prefer-destructuring) at this.message = res.response.data.errors[0]; at