Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this.name的值应该是一个不固定的值 #178

Open
Froguard opened this issue Apr 26, 2017 · 3 comments
Open

this.name的值应该是一个不固定的值 #178

Froguard opened this issue Apr 26, 2017 · 3 comments

Comments

@Froguard
Copy link

Froguard commented Apr 26, 2017

你好,如下代码中,如果想要在header组件的代码的this对象上,获取到name,目前获取不到,如果在header组件定义时候没写的话,是undefined

app.js

App.component('top-header', header);

header.js

let header = Regular.extend({
    //name: 'header',
    config(){
      console.log(this.name);// undefined
    },
    //...
    
});

期望结果:
header.js中的 this.name 将得到 top-header

@fengzilong
Copy link
Member

fengzilong commented Apr 27, 2017

可以加个$name方法,this.name可能和用户自己定义的this.name冲突了

@Froguard
Copy link
Author

Froguard commented May 2, 2017

@fengzilong
哦,这个到不单单是为了解决冲突的问题,而是想象中,我更倾向于觉得 this.name 应该设计成灵活的精确的或者叫自动的,亦或者说官方的,而不是开发者取巧的方式去实现它本来就应该提供的一个特性。作为库自己的一个特性存在。

类似的举个例子,js中function的name值会根据情况(例如function的定义写法不同,挂载对象,bind使用等等),而返回不同的值。这个是其特性,或者说叫原始设计。即使理论上说,开发者也是可以通过Object.defineProperty()这类方式去改变这个name值。但是这个灵活特性应该不由用户去实现。

这样说会不会有点偷懒耍赖皮,:smile:

@fengzilong
Copy link
Member

fengzilong commented May 2, 2017

@Froguard

我指的就是官方加啊,如果要加这个特性的话

但不能保证this.name没有被开发者使用,可能一些老代码会遇到一些意外的状况😥

看一个例子

const A = Regular.extend( {
    name: 'A',
    init() {
        console.log( this.name );
    }
} );

const B = Regular.extend().component( 'B', A );
new A( {
    name: 'C'
} );

this.name,应该是A、B还是C,这个优先级也要考虑

我还是偏向于加个this.$name方法用来获取组件名字而不是this.name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants