試試這個
一、Object.prototype.getName = function(){
s = this.constructor.toString();
if(s.indexOf("function") == -1){
return null;
}else{
s = s.replace("function","");
var idx = s.indexOf("(");
s = s.substring(0, idx);
s = s.replace(" ", "");
}
return s;
//除錯
function MyClass(){}
var x = new MyClass();
var name = x.getName();
alert(name);
2、
Object.prototype.getName = function() {
var funcNameRegex = /function (.{1,})\(/;
var results = (funcNameRegex).exec((this).constructor.toString());
return (results && results.length > 1) ? results[1] : "";
};
3、
function ExampleClass(){
ExampleClass.prototype.getName=function(){
for (var a in window){
if (window[a]==this){ alert(a.toString());
window.instance1=new ExampleClass();
instance1.getName();
試試這個
一、Object.prototype.getName = function(){
s = this.constructor.toString();
if(s.indexOf("function") == -1){
return null;
}else{
s = s.replace("function","");
var idx = s.indexOf("(");
s = s.substring(0, idx);
s = s.replace(" ", "");
}
return s;
}
//除錯
function MyClass(){}
var x = new MyClass();
var name = x.getName();
alert(name);
2、
Object.prototype.getName = function() {
var funcNameRegex = /function (.{1,})\(/;
var results = (funcNameRegex).exec((this).constructor.toString());
return (results && results.length > 1) ? results[1] : "";
};
3、
function ExampleClass(){
}
ExampleClass.prototype.getName=function(){
for (var a in window){
if (window[a]==this){ alert(a.toString());
};
}
}
window.instance1=new ExampleClass();
instance1.getName();