第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

是否可以在不使用eval的情況下在JavaScript中實(shí)現(xiàn)動(dòng)態(tài)作用域?

是否可以在不使用eval的情況下在JavaScript中實(shí)現(xiàn)動(dòng)態(tài)作用域?

SMILET 2019-11-15 12:42:23
JavaScript具有詞法作用域,這意味著從函數(shù)內(nèi)部訪問的非局部變量在定義時(shí)將解析為該函數(shù)的父級作用域中存在的變量。這與動(dòng)態(tài)作用域相反,在動(dòng)態(tài)作用域中,從函數(shù)內(nèi)部訪問的非局部變量在調(diào)用時(shí)將解析為該函數(shù)的調(diào)用范圍中存在的變量。x=1function g () { echo $x ; x=2 ; }function f () { local x=3 ; g ; }f # does this print 1, or 3?echo $x # does this print 1, or 2?上面的程序以詞法范圍的語言打印1,然后打印2,并且以動(dòng)態(tài)范圍的語言打印3,然后打印1。由于JavaScript具有詞法范圍,因此將顯示1,然后顯示2,如下所示:var print = x => console.log(x);var x = 1;function g() {    print(x);    x = 2;}function f() {    var x = 3;    g();}f();           // prints 1print(x);      // prints 2盡管JavaScript不支持動(dòng)態(tài)作用域,但我們可以使用eval以下方法實(shí)現(xiàn)它:var print = x => console.log(x);var x = 1;function g() {    print(x);    x = 2;}function f() {    // create a new local copy of `g` bound to the current scope    // explicitly assign it to a variable since functions can be unnamed    // place this code in the beginning of the function - manual hoisting    var g_ = eval("(" + String(g) + ")");    var x = 3;    g_();}f();                         // prints 3print(x);                    // prints 1我想知道是否存在另一種不求助于相同結(jié)果的可能方法eval。
查看完整描述

3 回答

?
拉風(fēng)的咖菲貓

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊

屬性查找貫穿整個(gè)原型鏈,該原型鏈與動(dòng)態(tài)范圍非常匹配。只需傳遞您自己的動(dòng)態(tài)范圍變量環(huán)境即可使用,而不是使用Javascript的詞法作用域。


// Polyfill for older browsers.  Newer ones already have Object.create.

if (!Object.create) {

  // You don't need to understand this, but

  Object.create = function(proto) {

    // this constructor does nothing,

    function cons() {}

    // and we assign it a prototype,

    cons.prototype = proto;

    // so that the new object has the given proto without any side-effects.

    return new cons();

  };

}

// Define a new class

function dyn() {}

// with a method which returns a copy-on-write clone of the object.

dyn.prototype.cow = function() {

  // An empty object is created with this object as its prototype.  Javascript

  // will follow the prototype chain to read an attribute, but set new values

  // on the new object.

  return Object.create(this);

}

// Given an environment, read x then write to it.

function g(env) {

  console.log(env.x);

  env.x = 2;

}

// Given an environment, write x then call f with a clone.

function f(env) {

  env.x = 3;

  g(env.cow());

}

// Create a new environment.

var env = new dyn();

// env -> {__proto__: dyn.prototype}

// Set a value in it.

env.x = 1;

// env -> {x: 1}  // Still has dyn.prototype, but it's long so I'll leave it out.


f(env.cow());

// f():

//   env -> {__proto__: {x: 1}}  // Called with env = caller's env.cow()

//   > env.x = 3

//   env -> {x: 3, __proto__: {x: 1}}  // New value is set in current object

//   g():

//     env -> {__proto__: {x: 3, __proto__: {x: 1}}}  // caller's env.cow()

//     env.x -> 3  // attribute lookup follows chain of prototypes

//     > env.x = 2

//     env -> {x: 2, __proto__: {x: 3, __proto__: {x: 1}}}


console.log(env.x);

// env -> {x: 1}  // still unchanged!

// env.x -> 1


查看完整回答
反對 回復(fù) 2019-11-15
?
哆啦的時(shí)光機(jī)

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊

要添加關(guān)于此主題的注釋:

在JavaScript中,只要您使用以下內(nèi)容:

  • 函數(shù)聲明語句或函數(shù)定義表達(dá)式,則局部變量將具有詞法范圍

  • 函數(shù)構(gòu)造函數(shù),然后局部變量將引用全局范圍(頂級代碼)

  • this 是JavaScript中唯一具有動(dòng)態(tài)作用域并通過執(zhí)行(或調(diào)用)上下文設(shè)置的內(nèi)置對象。

因此,要回答您的問題,在JS中this,該語言已經(jīng)是動(dòng)態(tài)作用域的功能,您甚至無需模仿其他功能。


查看完整回答
反對 回復(fù) 2019-11-15
?
明月笑刀無情

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊

為什么沒有人說this?


您可以通過將變量綁定到上下文來將變量從調(diào)用范圍傳遞到被調(diào)用函數(shù)中。


function called_function () {

   console.log(`My env ${this} my args ${arguments}`, this, arguments);

   console.log(`JS Dynamic ? ${this.jsDynamic}`);

}


function calling_function () {

   const env = Object.create(null);

   env.jsDynamic = 'really?';


   ... 


   // no environment

   called_function( 'hey', 50 );


   // passed in environment 

   called_function.bind( env )( 'hey', 50 );

也許值得一提的是,在嚴(yán)格模式下,默認(rèn)情況下,所有函數(shù)都不會(huì)發(fā)送“環(huán)境”(this為null)。在非嚴(yán)格模式下,全局對象是被this調(diào)用函數(shù)的默認(rèn)值。


查看完整回答
反對 回復(fù) 2019-11-15
  • 3 回答
  • 0 關(guān)注
  • 555 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號