<script type='text/javascript'>
function university(name,major,type){
//属性
this.name = name;
this.major = major;
this.type = type;
//方法
this.showUniversity = showUniversity1;
}
function showUniversity1(){
if(this.type == null || this.type == "DEFAULT"){
alert(this.name + this.major);
}
if(this.type == "UPPER"){
alert((this.name + this.major).toUpperCase());
}
if(this.type == "LOWER"){
alert((this.name + this.major).toLowerCase());
}
}var test = new university("ZheJiang Univeristy"," Computer School","UPPER");
test.showUniversity ();
</script>
function university(name,major,type){
//属性
this.name = name;
this.major = major;
this.type = type;
//方法
this.showUniversity = showUniversity1;
}
function showUniversity1(){
if(this.type == null || this.type == "DEFAULT"){
alert(this.name + this.major);
}
if(this.type == "UPPER"){
alert((this.name + this.major).toUpperCase());
}
if(this.type == "LOWER"){
alert((this.name + this.major).toLowerCase());
}
}var test = new university("ZheJiang Univeristy"," Computer School","UPPER");
test.showUniversity ();
</script>
你的写法是将type参数,写到构造函数里去,
但我要求type参数必须作为showUniversity1的形参。因为有些时候,某个javascript对象的某个方法是有参数的,
test = new university("ZheJiang Unveristy "," Computer School");
test.showUniversity("UPPER");
//属性
this.name=name;
this.major=major;
//方法
//this.showUniversity = function (TYPE) {showUniversity1(TYPE)};
this.showUniversity = showUniversity1;
}function showUniversity1(TYPE){
if(TYPE == "UPPER"){
alert("大写"+(this.name+this.major).toUpperCase());
}
if(TYPE == "LOWER"){
alert("小写"+(this.name+this.major).toLowerCase());
}
if(TYPE == null || TYPE =="DEFAULT"){
alert("默认"+this.name+this.major);
}
}var test = new university("ZheJiang Univeristy"," Computer School");
test.showUniversity ("UPPER");
test.showUniversity ("LOWER");
test.showUniversity ();</script>
function university(name,major){
//属性
this.name=name;
this.major=major;
//方法
this.showUniversity = function(type){
if(type == null || type =="DEFAULT"){
alert(this.name+this.major);
}
if(type == "UPPER"){
alert((this.name+this.major).toUpperCase();
}
if(type == "LOWER"){
alert((this.name+this.major).toLowerCase();
}
}
}
var test = new university("ZheJiang Univeristy"," Computer School");
test.showUniversity ();
</script>
function university(name,major){
//属性
this.name=name;
this.major=major;
//方法
this.showUniversity = function(){showUniversity.apply(this,arguments);}
}
function showUniversity(type){
if(type == null || type =="DEFAULT"){
alert(this.name+this.major);
}
if(type == "UPPER"){
alert((this.name+this.major).toUpperCase();
}
if(type == "LOWER"){
alert((this.name+this.major).toLowerCase();
}
}
var test = new university("ZheJiang Univeristy"," Computer School");
test.showUniversity ();
</script>
test.showUniversity ("UPPER");
test.showUniversity ("LOWER");
test.showUniversity ();
function university(name,major)
{
//属性
this.name=name;
this.major=major;
}
university.prototype.showUniversity=function(type)
{
var str=this.name+this.major;
switch(type)
{
case "UPPER": str=(this.name+this.major).toUpperCase(); break;
case "LOWER": str=(this.name+this.major).toLowerCase(); break;
}
alert(str);
}
var test = new university("ZheJiang Univeristy"," Computer School");
test.showUniversity("UPPER");
</script>
<html><head><script type='text/javascript'>
function university(name,major){
//属性
this.name=name;
this.major=major;
//方法
this.showUniversity = showUniversity1;
this.getUniversity = getUniversity1;
}
function showUniversity1(type){
myshow=getshow(this.getUniversity(),type);
alert(myshow);
return 0;
}
function getUniversity1(){
myreturn=this.name+this.major;
return myreturn;
}
function getshow(mystring,type){
if (type==null) type="";
switch(type){
case "":
case "DEFAULT":return mystring;break;
case "UPPER": return mystring.toUpperCase();break;
case "LOWER": return mystring.toLowerCase();break;
default:return "数据出错";
}
}
var test = new university("ZheJiang Univeristy"," Computer School");
test.showUniversity ();
</script></head><body></body></html>