搜索:
首页 >> JavaScript >> Js高级编程 >> javascript 的 in 操作符实例详解

javascript 的 in 操作符实例详解

2007-10-7 作者:xling 来源:蓝色理想 投递文章

以下摘录自 oreilly.JavaScript.The.Definitive.Guide.5th.Edition.Aug.2006

The in Operator

The in operator expects a left-side operand that is or can be converted to a string. It expects a right-side operand that is an object (or array). It evaluates to TRue if the left-side value is the name of a property of the right-side object. For example:

var point = { x:1, y:1 };        // Define an object
var has_x_coord = "x" in point;  // Evaluates to true
var has_y_coord = "y" in point;  // Evaluates to true
var has_z_coord = "z" in point;  // Evaluates to false; not a 3-D point
var ts = "toString" in point;    // Inherited property; evaluates to true
以下是我的总结,保存自本地运行:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body></body></html><script language="javascript" type="text/javascript">var aa = ["a","b",,"D"];document.write(’var aa = ["a","b",,"D"];’.link("Array"),"<br />");for(var i=-1;i<aa.length + 1;i++){ document.write(i.toString().fontcolor("#cc6600")," in aa : ",(i in aa).toString().italics(),"<br />");}
var bb = {name:"xling",sex:"male"}document.write(’var bb = {name:"xling",sex:"male"}’.link("Object"),"<br />");document.write( "name".fontcolor("#cc6600")," in bb :",("name" in bb).toString().italics(),"<br />", "sex".fontcolor("#cc6600")," in bb :",("sex" in bb).toString().italics(),"<br />", "birthday".fontcolor("#cc6600")," in bb :",("birthday" in bb).toString().italics(),"<br />");
var CC = function(){ this.name = "xling"; this.sex = "male"; this.birthday = "****";  this.getAge = function(){  return "***" }  var add = function(){  return ""; }}
var cc = new CC();document.write("var cc = new CC();".link("Instance of CC (Custom Class)"),"<br />");document.write( "name".fontcolor("#cc6600")," in cc :",("name" in cc).toString().italics(),"<br />", "sex".fontcolor("#cc6600")," in cc :",("sex" in cc).toString().italics(),"<br />", "birthday".fontcolor("#cc6600")," in cc :",("birthday" in cc).toString().italics(),"<br />", "getAge()".fontcolor("#cc6600")," in cc :",("getAge" in cc).toString().italics(),"<br />", "add()".fontcolor("#cc6600")," in cc :",("add" in cc).toString().italics(),"<br />");</script>
Tags:in  javascript  操作符 
相关文章
手机版 Js高级编程 Asp之家 Aspxhome.com
闽ICP备06017341号