如何在Javascript中动态设置功能/对象名称,因为它在Chrome中显示 [英] How to dynamically set a function/object name in Javascript as it is displayed in Chrome

查看:83
本文介绍了如何在Javascript中动态设置功能/对象名称,因为它在Chrome中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在研究一个Google Chrome浏览器调试器,这个问题一直困扰着我,并且我想知道是否有办法解决这个问题。大型Javascript应用程序,使用大量面向对象的JS(使用 Joose 框架),并且当我调试我的代码时,所有我的课程给了一个非感性的初始显示值。要明白我的意思,请在Chrome控制台中试用此功能:

  var F = function(){}; 
var myObj = new F();

console.log(myObj);

输出应该是一行,您可以展开以查看<$ c $的所有属性c> myObj ,但您首先看到的仅仅是▶F



我的问题是,由于我的面向对象框架,每个实例化的对象都获得相同的'名称'。它看起来是负责这样的代码是这样的:

  getMutableCopy:function(object){
var f = function(){};
f.prototype = object;
返回新的f();
}

这意味着在调试器中,初始视图总是<$ c $现在,我真的不想改变有关 的所有信息 Joose实例化了对象(getMutableCopy ...?),但如果有什么我可以添加到这个以便我可以提供我自己的名字,那就太好了。



我已经看过一些东西,但无法得到任何结果:

 >函数foo {} 
> foo.name
foo
> foo.name =bar
bar
> foo.name
foo//< - 看起来像只读


解决方案

  Object.defineProperty(fn,name,{value:New Name}); 

这样做会是最好的解决方案。没有评估。


This is something which has been bugging me with the Google Chrome debugger and I was wondering if there was a way to solve it.

I'm working on a large Javascript application, using a lot of object oriented JS (using the Joose framework), and when I debug my code, all my classes are given a non-sensical initial display value. To see what I mean, try this in the Chrome console:

var F = function () {};
var myObj = new F();

console.log(myObj);

The output should be a single line which you can expand to see all the properties of myObj, but the first thing you see is just ▶ F.

My issue is that because of my OO framework, every single object instantiated gets the same 'name'. The code which it looks is responsible for this is like so:

getMutableCopy : function (object) {
    var f = function () {};
    f.prototype = object;
    return new f();
}

Which means that in the debugger, the initial view is always ▶ f.

Now, I really don't want to be changing anything about how Joose instantiates objects (getMutableCopy...?), but if there was something I could add to this so that I could provide my own name, that would be great.

Some things that I've looked at, but couldn't get anywhere with:

> function foo {}
> foo.name
  "foo"
> foo.name = "bar"
  "bar"
> foo.name
  "foo"    // <-- looks like it is read only

解决方案

Object.defineProperty(fn, "name", { value: "New Name" });

Will do the trick and is the most performant solution. No eval either.

这篇关于如何在Javascript中动态设置功能/对象名称,因为它在Chrome中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆