//编写一个方法queryURLParmeter,实现把一个URL地址问号传参部分的信息获取到,并且解析称为对象键值对的方式,
//URL地址:
//'https://www.baidu.com/s?wd=javascript&rsv_spt=1'
//解析后结果为:
//{wd:'javascript',rsv_spt:1}
//方法一
let url = 'https://www.baidu.com/s?wd=javascript&rsv_spt=1';
//->indexof :检测出当前字符在字符串中第一次出现的位置
//->split:按照某一个字符把字符串拆分成数组中的每一项
// charAt charCodeAt substr substring slice replice mach
function queryURLParmeter(url) {
let obj = {};
if (url.indexOf('?') < 0)return obj;
let ary = url.split('?');
url = ary[1];
ary = url.split('&');
for (let i = 0; i < ary.length; i++) {
let cur = ary[i],
curAry = cur.split('=');
obj[curAry[0]] =curAry[1];
}
return obj;
}
let obj1 = queryURLParmeter(url);
1
console.log(a);
var a = 12;
function fn() {
console.log(a);
var a = 13;
}
fn();
console.log(a);
/*
A undefined 12 13
B undefined undefined 12 对
C undefined undefined 13
D 有程序报错
*/
2
console.log(a);
var a = 12;
function fn() {
console.log(a);
a = 13;
}
fn();
console.log(a);
/*
A undefined 12 13 对
B undefined undefined 12
C undefined undefined 13
D 有程序报错
*/
3
console.log(a);
a = 12;
function fn() {
console.log(a);
a = 13;
}
fn();
console.log(a);
/*
A undefined 12 13 对
B undefined undefined 12
C undefined undefined 13
D 有程序报错 对
*/
4
var foo =1;
function bar() {
if (!foo){// !undefined - true 成立
var foo = 10;
}
console.log(foo);
}
bar();
/*
A 1
B 10 对
C undefined
D
*/
5
//全局下的变量提升 var n ; var c; a = xxxfff000;
var n = 0;
function a() {
//私有作用域:无形参赋值 var n; b = xxxfff111;
var n = 10;// ->11
function b() {
//私有作用域:
n++;//n是上级作用域中的 本次n为11
console.log(n);//11
}
b();
return b;//return bbbfff111;
}
var c = a(); // bbbfff111
c(); // 12
console.log(n); // 0
/*
A 1 1 1
B 11 11 0
C 11 12 0 对
D 11 12 12
*/
6
//变量提升 var a ;var b; var c; test = aaaffff000;
var a = 10,b = 11, c = 12;
function test(a) {
//test执行 形成私有作用域 a = 10; var b;
a = 1;//私有A = 1
var b = 2;//私有 b = 2;
c = 3;// 全局 c = 3;
}
test(10);
console.log(a); //10
console.log(b); //11
console.log(c); //3
/*
A 1 11 3
B 10 11 12
C 1 2 3
D 10 11 3 对
*/
7
//变量提升 var a ; <=>window.a = undefined
if (!("a" in window)){//条件不成立 "a" in window ->true
var a = 1;
}
console.log(a);
/*
A 1
B undefined 对
C 报错
D 以上答案都不对
*/
8
// 全局下的变量提升:var a ; b=xxxfff000;
var a = 4;
function b (x,y,a) {
//形成私有作用域:x = 1,y =2,a = 3
console.log(a);//3
arguments[2] = 10;//让第三个传递进来的实参等于10
console.log(a);// 10
}
a = b(1,2,3); // undefined 因为b执行没有返回值
console.log(a);//undefined
/*
* 在JS的非严格模式下
* 函数的实参集合与形参变量存在"映射"关系:不管其中谁改变了,另外一个都会跟着发生改变
* 在JS严格模式下
* arguments和形参变量之间的映射关系切断,相互不干扰
*/
/*
A 3 3 4
B 3 10 4
C 3 10 10
D 3 10 undefined 正确
*/
9
var a = 9;
function fn() {
a = 0;
return function(b) {
return b+a++;
}
}
var f = fn();
console.log(f(5));
console.log(fn()(5));
console.log(f(5));
console.log(a);