我已经给出了一些关于声明一个名为undefined
的变量并用它来评估一个真实的if-else表达式的想法。我发现它在全球范围内是不可能的。但在MDN中,我发现了一些有关var person = {
method : function(){
var undefined = 2
if(undefined){
console.log("undefined works as a variable")
}
}
}
person.method()
数据类型的有趣见解。我直接引用MDN
undefined是全局对象的属性;即,它是一个变量 在全球范围内。
它没有说明关于本地范围的任何事情。这意味着我可以在本地范围内创建一个。所以,我继续将这种见解进行测试。我创建了一个对象并为其指定了一个方法
console.log()
猜猜是什么! if语句通过了测试,undefined
内的字符串被打印在控制台上。这可能是一件危险的事情,当然也是一种不好的做法。有没有办法阻止在javascript的本地范围内声明以exception.txt
关键字命名的变量?谢谢!
答案 0 :(得分:3)
要解决undefined
的意外修改,你不应该在你的代码中写下这个危险的词。
由于您只需要undefined
的读取权限,因此建议总是使用void 0
,而返回undefined
。
var person = {
method: function() {
var undefined = 2
if(void 0) {
console.log("this should never be printed")
} else {
console.log("void 0 is always undefined, no matter what you do")
}
}
}
person.method()
如何使用void 0
并完全摆脱“未定义”这个词?
// Turn
if(myVariable === undefined) { ... }
if(typeof myVariable === "undefined") { ... }
// into
if(myVariable === void 0) { ... }
// Turn
if((myVariable === undefined) || (myVariable === null)) { ... }
// into
if(myVariable == null) { ... }
// Turn
myObject.someKey = undefined
// into
myObject.someKey = void 0
答案 1 :(得分:2)
欢迎来到精彩的JavaScript世界!
没有办法阻止某人这样做,但是如果你按照以下方式设置你的功能,有一种方法可以确保undefined
意味着undefined
(不是你真的应该这样做)这样做是因为任何人实际设置名为undefined
的变量都是非常糟糕的做法。基本上较小的范围函数可以隐藏更高范围的undefined
变量。
// This is just a way to get a variable called "undefined"
function wrapper(){
var undefined = 10;
console.log("wrapper says undefined is: " + undefined);
// This function declared a parameter called "undefined",
// but if the function gets called with no argument, then
// the value of this, more local, argument will truly be
// undefined. If arguments are needed, just add "undefined"
// to the end of the list.
function foo(undefined){
// Formal test to ensure that undefined is what we believe it to be:
if(typeof undefined === "undefined"){
console.log("foo says undefined is: " + undefined);
}
}
// When foo is called with no arguments passed in, the parameter
// "undefined" will take on a value of undefined within the scope
// of that function.
foo();
}
wrapper();

现在,由于你不打算使用"假的"设置你的所有功能,这有点做作。参数,但您可以测试undefined
是否已被更改:
function wrapper(){
var undefined = 10;
console.log(undefined);
function foo(){
if(typeof undefined === "undefined"){
// undefined has not been altered
} else {
// undefined has been altered, so reset it for this scope:
let undefined;
console.log(undefined);
}
}
foo();
}
wrapper();

最后,您可以通过在函数中添加var undefined
来阻止此功能影响您的功能。无论你在哪里声明它,吊装都会确保它在你的功能顶部开始。
答案 2 :(得分:1)
我无法看到任何阻止它的方法,但是,你可以限制它通过yes inside如果你可以使用ES6使范围本地化。
你会发现现在如何改变范围,这不是一回事。
class DerexlUniversity(scrapy.Spider):
name = 'drexel_university'
allowed_domains = ['drexel.edu']
start_urls = ['http://drexel.edu/search?t=people&q=']
def parse(self, response):
# with open('kw.txt') as file_object:
# last_names = file_object.readlines()
for ln in ['Chong', 'Zhao']:
last_name = ln.strip()
print('-----------------------------------------------------')
print("scraping last name: ", last_name)
query = response.url + last_name
yield scrapy.Request(query, callback=self.parse_item, meta=dict(last_name=last_name))
def parse_item(self, response):
last_name = response.meta['last_name']
self.logger.info('This is item page %s', last_name)
result_rows = response.xpath('//table//tr[@class="result-row"]')
result_details = response.xpath('//table//tr[@class="result-details"]')
for row, detail in zip(result_rows, result_details):
full_name = row.xpath('.//span[@class="fullname"]/text()').extract_first()
if full_name:
full_name = clean_full_name(full_name)
if last_name in full_name.split():
item = {}
item['fullname'] = full_name
item['university'] = 'Drexel University'
try:
item['email'] = row.xpath('.//span[@class="email-address"]/a/@href').extract_first()[7:]
item['phone'] = row.xpath('.//span[@class="phone-numbers"]/a/@href').extract_first()[4:]
person_detail = detail.xpath('.//span[@class="person-detail"]/text()').extract()
except ValueError:
pass
else:
person_detail_clean = ', '.join([pd.strip() for pd in person_detail[0].split(',')][1:])
item['person_detail'] = person_detail_clean
yield item