JavaScript中特有语句 for in 和 with 语句介绍

本文详细解析JavaScript中的for-in循环和with语句的使用方法,包括如何遍历对象属性、确定作用范围以及如何通过with简化代码。通过实例演示,帮助开发者更好地理解和运用这些内置特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


for   in  语句

<script type="text/javascript">
	 /*js中的特有语句
	 for  in循环,用于遍历变量和对象的循环,相当于java中的高级for循环
	 格式:for(变量 in  对象){}
	 */
	function Person(){
		this.name="aaaaaa";//注意要用this,不能用var 声明,
		this.age=33;
		this.getName=function(){
			return this.name;
		}
	}
	var p=new Person();
	
	for(x in p){
		//alert(x);//此时获取的全是p对象中的各个属性
		alert(x+"----"+p[x]);//用p[x]这种方式获取每个属性具体的值,因为x为变量
	
	}
	alert("name"+"------------------------"+p.name);//这样获取是可以的,因为name是常量
	//alert("name================="+p[name]);//因为name为常量,所以不可以这样获取
	alert("name+++++++++++++++++++++"+p["name"]);//因为name上加了单引号或双引号,这样把name变成常量,就可以用这种方式获取了

	var arr=[2,44,55,7];//随便定义一个数组
	for(i in arr){
		alert(i);//打印的是角标
		alert(i+"------------"+arr[i]);//这样打印的是具体的值
	}

</script>

with确定作用范围

<script type="text/javascript">

	var date=new Date();
	//每次使用该对象中的方法,都需要用对象名调用,可以使用with解决该问题
	//格式:with(对象){},说明,确定了对象的作用范围,在该范围内,可以直接使用对象中的方法
	var d=(date.getMonth()+1)+"月"+date.getDate()+"日"//获取 月 日
	alert(d);
	with(date){
		var dd=getYear()+"年"+(getMonth()+1)+"月"+getDate()+"日";//用with这么写的话,可以简便,你懂的
	}
	alert(dd);
	//利用with这个特点,可以这样用
	with(Math){
		alert(random());//可以通过世界with(对象),调用其中的方法,爽
	}
</script>


``` var getQABits = function(image, start, end, newName) { // Compute the bits we need to extract. var pattern = 0; for (var i = start; i <= end; i++) { pattern += Math.pow(2, i); } // Return a single band image of the extracted QA bits, giving the band // a new name. return image.select([0], [newName]) .bitwiseAnd(pattern) .rightShift(start); }; // A function to mask out cloudy pixels. var maskClouds = function(image) { // Select the QA band. var QA = image.select('QA'); // Get the MOD_LAND_QA bits var internalCloud = getQABits(QA, 0, 1, 'MOD_LAND_QA'); // Return an image masking out cloudy areas. return image.mask(internalCloud.eq(0)); }; var modcloudmask=modsr.map(maskClouds); var modndvi=modcloudmask.map(function(img){ return img.normalizedDifference(['sur_refl_b02','sur_refl_b01']).rename('modNDVI') .resample('bilinear') .copyProperties(img, global_prop); // .copyProperties(img,['id']); }); //print('modndvi:',modndvi); //Map.addLayer(modndvi,{},'modNDVI') var modwdrvi=modndvi.map(function(img){ return img.expression('(1.2*b()-0.8)/(-0.8*b()+1.2)') //.set('system:time_start', image.get('system:time_start')) .rename('modWDRVI') // .copyProperties(img,[time]) .copyProperties(img, global_prop) }); //print('modwdrvi:',modwdrvi) //Map.addLayer(modwdrvi,{},'modWDRVI') var maizeGrowth_Filter = ee.Filter.calendarRange(167, 293,'day_of_year'); //167~June 15 (6, 10,'month') var wheatGrowth_Filter = ee.Filter.calendarRange(294, 166,'day_of_year'); //294~Oct 20 (11, 5,'month') // print(wheatGrowth_Filter);bug var modLai_maize = modwdrvi.filter(maizeGrowth_Filter) .map(function(img){ return img.expression('6.5*(b()-(-0.60))/(0.77-(-0.60))') //5, 6.5 .rename('modLai_Maize') .copyProperties(img, global_prop) .set('variety', 'maize') }); var modLai_wheat = modwdrvi.filter(wheatGrowth_Filter) .map(function(img){ return img.expression('6.5*(b()-(-0.60))/(0.43-(-0.60))') //.set('system:time_start', image.get('system:time_start')) .rename('modLai_Wheat') .copyProperties(img, global_prop) .set('variety', 'wheat') }); print('modLai_maize:',modLai_maize) print('modLai_wheat:',modLai_wheat)```逐行解释代码意思
最新发布
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

King·Forward

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值