温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ECMAScript语句之with 语句

发布时间:2020-07-17 09:11:44 来源:网络 阅读:338 作者:Gendan5 栏目:开发技术

ECMAScript with 语句,用于设置代码在特定对象中的作用域(with运行缓慢,设置了属性值时更加缓慢,最好避免使用with语句)

一、with 语句用于字符串(配合toUpperCase()方法)

var a = "CGLweb前端";
with(a) {
console.log(toUpperCase()); //输出 "CGLweb前端"
}

二、with 语句可以方便地用来引用某个特定对象中已有的属性,但是不能用来给对象添加属性。要给对象创建新的属性,必须明确地引用该对象

function xinxi() {
this.name = "青格勒";
this.age = "28";
this.gender = "男";
}
var people=new xinxi();
with(people)
{
var str = "姓名: " + name;
str += "、年龄:" + age;
str += "、性别:" + gender;
console.log(str);
}

三、with语句中的对象不是作为执行环境添加到作用域中,而是执行环境之中作用的

var obj1 = [
{a: 11},
{c: 12}
];
function cgl() {(www.gendna5.com)
var a = 2;
with (obj1) {
{a = 3};
{c = 4};
}
console.log(a); //3
console.log(c); //4
console.log(obj1); //[ { a: 11 }, { c: 12 } ]
console.log(obj1[0].a); //11
console.log(obj1[1].c); //12
}
cgl();
console.log(obj1[0].a); //11
console.log(obj1[1].c); //12

这个因为资料有限就说道这里吧。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI