日期:2014-05-16  浏览次数:20469 次

js对象内怎么复制属性
比如有对象
JScript code

var obj = {
    companyname:function(option){
        var format    = validateRules.isCompanyname(option.value);
        var length    = validateRules.betweenLength(option.value,1,50);
        var opt        = option;
        if(!format || !length){
            if(!format) opt.errInfo = option.prompts.error.badFormat;
            if(!length) opt.errInfo = option.prompts.error.badLength;
            validateFunction.errorfunc(opt);
        }else{
            validateFunction.succefunc(option);
        }        
    }//companyname end
    ,companyaddr:function(option){
        var format    = validateRules.isCompanyname(option.value);
        var length    = validateRules.betweenLength(option.value,1,50);
        var opt        = option;
        if(!format || !length){
            if(!format) opt.errInfo = option.prompts.error.badFormat;
            if(!length) opt.errInfo = option.prompts.error.badLength;
            validateFunction.errorfunc(opt);
        }else{
            validateFunction.succefunc(option);
        }
    }
}



因为companyname和companyaddr属性是一样的函数,我想简写为
JScript code

var obj = {
    companyname:function(option){
        var format    = validateRules.isCompanyname(option.value);
        var length    = validateRules.betweenLength(option.value,1,50);
        var opt        = option;
        if(!format || !length){
            if(!format) opt.errInfo = option.prompts.error.badFormat;
            if(!length) opt.errInfo = option.prompts.error.badLength;
            validateFunction.errorfunc(opt);
        }else{
            validateFunction.succefunc(option);
        }        
    }//companyname end
    ,companyaddr:this.companyname
}



改了后提示object is not a function,不能正确引用函数

js对象内怎么复制属性?
在线等


------解决方案--------------------
在内部写是不行的,因为obj是一个数组对象,这样写是可以的:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script>
 var obj = {
companyname:function(option){
var format = validateRules.isCompanyname(option.value);
var length = validateRules.betweenLength(option.value,1,50);
var opt = option;
if(!format || !length){
if(!format) opt.errInfo = option.prompts.error.badFormat;
if(!length) opt.errInfo = option.prompts.error.badLength;
validateFunction.errorfunc(opt);
}else{
validateFunction.succefunc(option);
}
}//companyname end
,companyaddr:function(option){}
}
obj.companyaddr = obj.companyname;
</script>
 </head>

 <body>
<input type="button" id="btnTest" onclick="obj.companyname('companyname')" value="测试1"><br/>
<input type="button" id="btnTest" onclick="obj.companyaddr('companyaddr')" value="测试2">
 </body>
</html>
------解决方案--------------------
JScript code

var obj = {
    companyname:function(option){
        var format