shadowing

Why does shadowed variable evaluate to undefined when defined in outside scope?

纵然是瞬间 提交于 2019-11-26 11:35:16
问题 Consider the following piece of code: <html><head></head> <body> <script type=\"text/javascript\"> var outside_scope = \"outside scope\"; function f1() { alert(outside_scope) ; } f1(); </script> </body> </html> The output for this code is that the alert box displays the message \"outside scope\". But, if I slightly modify the code as: <html><head></head> <body> <script type=\"text/javascript\"> var outside_scope = \"outside scope\"; function f1() { alert(outside_scope) ; var outside_scope = \

What is Shadowing?

余生颓废 提交于 2019-11-26 03:38:28
问题 In C# what does the term shadowing mean? I have read this link but didn\'t fully understand it. 回答1: Shadowing hides a method in a base class. Using the example in the question you linked: class A { public int Foo(){ return 5;} public virtual int Bar(){return 5;} } class B : A { public new int Foo() { return 1;} public override int Bar() {return 1;} } Class B overrides the virtual method Bar . It hides (shadows) the non-virtual method Foo . Override uses the override keyword. Shadowing is

What is variable shadowing used for in a Java class?

一笑奈何 提交于 2019-11-26 03:17:04
问题 I\'m reading my Deitel, Java How to Program book and came across the term shadowing . If shadowing is allowed, what situation or what purpose is there for it in a Java class? Example: public class Foo { int x = 5; public void useField() { System.out.println(this.x); } public void useLocal() { int x = 10; System.out.println(x); } } 回答1: The basic purpose of shadowing is to decouple the local code from the surrounding class. If it wasn't available, then consider the following case. A Class Foo

An example of variable shadowing in javascript

大兔子大兔子 提交于 2019-11-26 01:00:40
问题 I learnt about the term variable shadowing in Eloquent Javascript (Chapter 3), but I am trying to understand a precise, basic example of the concept. Is this an example of shadowing? var currencySymbol = \"$\"; function showMoney(amount) { var currencySymbol = \"€\"; document.write(currencySymbol + amount); } showMoney(\"100\");​ 回答1: That is also what is known as variable scope . A variable only exists within its containing function/method/class, and those will override any variables which

Importing installed package from script raises “AttributeError: module has no attribute” or “ImportError: cannot import name”

喜夏-厌秋 提交于 2019-11-25 21:34:21
问题 I have a script named requests.py that imports the requests package. The script either can\'t access attributes from the package, or can\'t import them. Why isn\'t this working and how do I fix it? The following code raises an AttributeError . import requests res = requests.get(\'http://www.google.ca\') print(res) Traceback (most recent call last): File \"/Users/me/dev/rough/requests.py\", line 1, in <module> import requests File \"/Users/me/dev/rough/requests.py\", line 3, in <module>