M
Michael Haufe (\TNO\)
I'm attempting to use a design pattern for creating some constructor
functions, but I'm running into a bugbear of verbosity that I hope
someone could enlighten me with in re-factoring towards a more
straightforward solution.
Here's an example of one of the constructors using the design:
-------------------------------------
function XmlDeclaration(version, encoding, standalone) {
var _encoding,
_len = arguments.length,
_standalone,
_version;
if(_len !== 3 && _len !== 1) {
throw new Error("Invalid arguments");
} else if(version instanceof XmlDeclaration ){
_encoding = version.encoding;
_standalone = version.standalone;
_version = version.version;
} else if(typeof version === "string" &&
typeof encoding === "string" &&
typeof standalone === "string") {
_encoding = encoding;
_version = version;
_standalone = standalone;
} else {
throw new TypeError("Invalid arguments");
}
this.__defineGetter__("encoding",function() {
return _encoding
})
this.__defineSetter__("encoding",function(v) {
if(typeof v === "string") {
_encoding = v
} else {
throw new TypeError("Invalid property assignment. A string is
expected.")
}
})
this.__defineGetter__("standalone",function()
return _standalone
})
this.__defineSetter__("standalone",function(v){
if(typeof v === "string"){
_standalone = v
}else{
throw new TypeError("Invalid property assignment. A string is
expected.")
}
})
this.__defineGetter__("version",function(){
return _version
})
this.__defineSetter__("version",function(v){
if(typeof v === "string"){
_version = v
}else{
throw new TypeError("Invalid property assignment. A string is
expected.")
}
})
this.toString = function(){
return "<" + "?xml version=\"" + _version + "\" encoding=\"" +
_encoding + "\" standalone=\"" + _standalone + "\"?" + " >"
}
}
var foo = new XmlDeclaration("1.0", "utf-8", "yes")
foo.encoding = "utf-16"
foo.toString() //<?xml version="1.0" encoding="utf-16"
standalone="yes" ?>
functions, but I'm running into a bugbear of verbosity that I hope
someone could enlighten me with in re-factoring towards a more
straightforward solution.
Here's an example of one of the constructors using the design:
-------------------------------------
function XmlDeclaration(version, encoding, standalone) {
var _encoding,
_len = arguments.length,
_standalone,
_version;
if(_len !== 3 && _len !== 1) {
throw new Error("Invalid arguments");
} else if(version instanceof XmlDeclaration ){
_encoding = version.encoding;
_standalone = version.standalone;
_version = version.version;
} else if(typeof version === "string" &&
typeof encoding === "string" &&
typeof standalone === "string") {
_encoding = encoding;
_version = version;
_standalone = standalone;
} else {
throw new TypeError("Invalid arguments");
}
this.__defineGetter__("encoding",function() {
return _encoding
})
this.__defineSetter__("encoding",function(v) {
if(typeof v === "string") {
_encoding = v
} else {
throw new TypeError("Invalid property assignment. A string is
expected.")
}
})
this.__defineGetter__("standalone",function()
return _standalone
})
this.__defineSetter__("standalone",function(v){
if(typeof v === "string"){
_standalone = v
}else{
throw new TypeError("Invalid property assignment. A string is
expected.")
}
})
this.__defineGetter__("version",function(){
return _version
})
this.__defineSetter__("version",function(v){
if(typeof v === "string"){
_version = v
}else{
throw new TypeError("Invalid property assignment. A string is
expected.")
}
})
this.toString = function(){
return "<" + "?xml version=\"" + _version + "\" encoding=\"" +
_encoding + "\" standalone=\"" + _standalone + "\"?" + " >"
}
}
var foo = new XmlDeclaration("1.0", "utf-8", "yes")
foo.encoding = "utf-16"
foo.toString() //<?xml version="1.0" encoding="utf-16"
standalone="yes" ?>