当前位置: 代码迷 >> JavaScript >> JS面向对象入门(jquery思维)
  详细解决方案

JS面向对象入门(jquery思维)

热度:263   发布时间:2012-10-06 17:34:01.0
JS面向对象入门(jquery思想)
<html> 
<head> 
<TITLE>class_obj_js_class</TITLE> 
<script language=javaScript> 
var userObj = function(){ 

}; 
userObj.prototype={ 
    age:0, 
    name:"huangbiao", 
    getUserName:function(){ 
        return this.name; 
    }, 
    getUserAge:function(){ 
        return this.age; 
    } 
} 

function getUserName(){ 
    alert(userObj.prototype.getUserName()); 
} 

function getUserAge(){ 
    alert(userObj.prototype.getUserAge()); 
} 

function newObj(){
//如果new了一个对象就可以直接访问prototype对象
	var newObj = new userObj();
	var userName = newObj.getUserName();
	alert(userName);
	var userAge = newObj.getUserAge();
	alert(userAge);
}

</script> 

<body > 
    <input type="button" value="getName" onclick="getUserName();"></input> 
	<br>
    <input type="button" value="getUserAge" onclick="getUserAge();"></input>
	<br>
    <input type="button" value="getUserAge" onclick="newObj();"></input>
</body> 
</html>

 

?

  相关解决方案