lexical-closures

Prevent JavaScript closure from inheriting scope

爱⌒轻易说出口 提交于 2019-11-29 04:43:53
I am looking for a fancy way to prevent a closure from inheriting surrounding scrope. For example: let foo = function(t){ let x = 'y'; t.bar = function(){ console.log(x); // => 'y' }); }; there are only two ways I know of preventing sharing scope: (1) Use shadow variables: let foo = function(t){ let x = 'y'; t.bar = function(x){ console.log(x); // => '?' }); }; (2) Put the function body somewhere else: let foo = function(t){ let x = 'y'; t.bar = createBar(); }; My question is - does anyone know of a 3rd way to prevent closures from inheriting scope in JS? Something fancy is fine. The only

Accessing VUE JS's data from Axios

白昼怎懂夜的黑 提交于 2019-11-28 23:25:51
问题 I have a Vue JS (Vuetify) App that makes an ajax request that I would like to populate a div's content with the response, however I am having difficulties accessing the instance's data . All examples I have seen use this to point to the data object, but when I do I get this error Unable to set property 'message' of undefined or null reference The app is quite simple: main.js : import Vue from 'vue' import App from './App.vue' import Vuetify from 'vuetify' Vue.use(Vuetify) new Vue({ el: '#app'

Lexical scope/closures in javaScript

允我心安 提交于 2019-11-28 16:55:23
I understand functions in 'js' have lexical scope (i.e. functions create their environment (scope) when they are defined not when they are executed.) function f1() { var a = 1; f2(); } function f2() { return a; } f1(); // a is not defined When I run just 'f()' it returns the inner function. Which I get, that's what 'return' does! function f() { var b = "barb"; return function() { return b; } } console.log(b); //ReferenceError: b is not defined Why do you get 'ReferenceError: b is not defined?' But doesn't the inner function above have access to it's space, f()'s space etc. Being that 'b' is

Emacs lisp: why does this sexp cause an invalid-function error?

走远了吗. 提交于 2019-11-28 03:54:45
问题 The sexp in question is (((lambda (b) (lambda (a) (+ b a))) 3) 5) which, to me, looks like it should evaluate to 8 , and in other lisps (e.g. Racket) it does, but in elisp it instead throws this error: Debugger entered--Lisp error: (invalid-function ((lambda (b) (lambda (a) (+ b a))) 3)) It appears to be telling me that ((lambda (b) (lambda (a) (+ b a))) 3) Is not a valid function. This seems wrong, because when I evaluate that expression I get (lambda (a) (+ b a)) which looks like a valid

Prevent JavaScript closure from inheriting scope

北城以北 提交于 2019-11-27 18:41:11
问题 I am looking for a fancy way to prevent a closure from inheriting surrounding scrope. For example: let foo = function(t){ let x = 'y'; t.bar = function(){ console.log(x); // => 'y' }); }; there are only two ways I know of preventing sharing scope: (1) Use shadow variables: let foo = function(t){ let x = 'y'; t.bar = function(x){ console.log(x); // => '?' }); }; (2) Put the function body somewhere else: let foo = function(t){ let x = 'y'; t.bar = createBar(); }; My question is - does anyone

Lexical scope/closures in javaScript

為{幸葍}努か 提交于 2019-11-27 09:53:59
问题 I understand functions in 'js' have lexical scope (i.e. functions create their environment (scope) when they are defined not when they are executed.) function f1() { var a = 1; f2(); } function f2() { return a; } f1(); // a is not defined When I run just 'f()' it returns the inner function. Which I get, that's what 'return' does! function f() { var b = "barb"; return function() { return b; } } console.log(b); //ReferenceError: b is not defined Why do you get 'ReferenceError: b is not defined?