Javascript:类方法中的实例名称 函数jExgTrend(){ } jExgTrend.prototype.Start=函数(文本) { //这必须返回实例名称:“TestObj” var InstanceName=“TestObj”; document.getElementById(“输出”).innerHTML=“”; } jExgTrend.prototype.Notify=函数(msg) { 警报(msg); } var TestObj=新的jExgTrend(); TestObj.Start(“链接文本”);
我怎么能做这样的事?方法“Start”应该返回类实例的名称Javascript:类方法中的实例名称 函数jExgTrend(){ } jExgTrend.prototype.Start=函数(文本) { //这必须返回实例名称:“TestObj” var InstanceName=“TestObj”; document.getElementById(“输出”).innerHTML=“”; } jExgTrend.prototype.Notify=函数(msg) { 警报(msg); } var TestObj=新的jExgTrend(); TestObj.Start(“链接文本”);,javascript,class,object,Javascript,Class,Object,我怎么能做这样的事?方法“Start”应该返回类实例的名称 这个问题很愚蠢,我知道:-(你不能。你可以在实例化时指定一个名称,不过: <html> <body> <div id="output"></div> <script> function jExgTrend(){ } jExgTrend.prototype.Start = function(text) { //this mus
这个问题很愚蠢,我知道:-(你不能。你可以在实例化时指定一个名称,不过:
<html>
<body>
<div id="output"></div>
<script>
function jExgTrend(){
}
jExgTrend.prototype.Start = function(text)
{
//this must return Instance name : "TestObj"
var InstanceName = "TestObj";
document.getElementById("output").innerHTML = "<a href=\"javascript:"+InstanceName+".Notify('"+text+"');\">"+text+"</a>";
}
jExgTrend.prototype.Notify = function(msg)
{
alert(msg);
}
var TestObj = new jExgTrend();
TestObj.Start("Text of the link");
</script>
</body>
</html>
一个有点奇怪的选择:您可以像这样编程构造函数:
function JExgTrend(name){ this.name = name || 'no name specified'; }
JExgTrend.prototype.Start = function () {
alert(this.name);
}
var testObj = new JExgTrend('testObj');
var otherTestObj = new JExgTrend('otherTestObj');
var anon = new JExgTrend;
testObj.Start(); //=> testObj
otherTestObj.Start(); //=> otherTestObj
anon.Start(); //=> no name specified
function JExgTrend(name,scope) {
name = name || ( Math.floor( 10000+Math.random()*100000000000) ).toString(16);
if (!(this instanceof JExgTrend)) {
return new JExgTrend(name,scope);
}
this.name = name;
if (!JExgTrend.prototype.myname) {
JExgTrend.prototype.myname = function(){ console.log(this.name); };
}
return (scope || window)[this.name] = this;
}
然后指定如下对象:
function JExgTrend(name){ this.name = name || 'no name specified'; }
JExgTrend.prototype.Start = function () {
alert(this.name);
}
var testObj = new JExgTrend('testObj');
var otherTestObj = new JExgTrend('otherTestObj');
var anon = new JExgTrend;
testObj.Start(); //=> testObj
otherTestObj.Start(); //=> otherTestObj
anon.Start(); //=> no name specified
function JExgTrend(name,scope) {
name = name || ( Math.floor( 10000+Math.random()*100000000000) ).toString(16);
if (!(this instanceof JExgTrend)) {
return new JExgTrend(name,scope);
}
this.name = name;
if (!JExgTrend.prototype.myname) {
JExgTrend.prototype.myname = function(){ console.log(this.name); };
}
return (scope || window)[this.name] = this;
}
试着摆弄一下@也许你真正需要的是一个id,而不是一个名字。
提供一个id可以很容易地通过一个方法添加,该方法可以添加到对象的原型中,或者只添加到您感兴趣的类中:
jExgTrend.call(null, 'testObj');
testObj.myname(); //=> testObj
使用的小示例:
var idGetter = (function() {
var currentId = 0;
return function() {
// 1. replace 'id' with a readonly property
// that will return id for this object
var thisId = currentId ;
Object.defineProperty( this, 'id',
{ get : function () { return thisId; } } ) ;
// 2. for the first run of id, return object id
return currentId++;
}
}());
Object.defineProperty( Object.prototype, 'id', { get : idGetter } );
请注意Object.defineProperty默认为不可枚举属性,因此您的对象不会被此属性“污染”(例如,当用于..时)。这可能是我已经使用过的解决方案的副本…但我不喜欢它:-(@epasineti:在答案中添加了一个JSFIDLE链接)“也许你真正需要的是一个id,而不是一个名称。”这段代码将为所有对象添加“id”属性,每个对象都将使用一个唯一的数字进行标识。这取决于你计划如何处理对象的名称/id。如果没有用,我将删除答案。