javascript動態生成方法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">javascript

<html xmlns="http://www.w3.org/1999/xhtml">html

<head>java

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />數據結構

<title>動態代碼生成測試</title>app

</head>測試

<div id="divtest" ></div>ui

<body>this

</body>spa

<script language="javascript" type="application/javascript" src="../js/closure.js"></script>xml

</html>

          

----js--

// JavaScript Document

var divtest = document.getElementById("divtest");

divtest.style.border="1px solid rgb(0,0,0)";


function User(properties)

{

for(var i in properties)

{

(

function(which)

{

var p =i;

which["get"+p]=function()

{

return properties[p];

}

which["set"+p]=function(val)

{

properties[p]=val;

}

}

)(this);

}

}  

var userObj = new User(

  {

name:"zxg",

age:27,

address:"100add"

}

  );

divtest.innerHTML = userObj.getname();

注意這裏傳入的參數的數據結構;

相關文章
相關標籤/搜索