html显示undefined,html - Javascript var returning "undefined" - Stack Overflow

本文探讨了如何在JavaScript中为构造函数分配属性,并讨论了不同情况下属性分配的方式,包括使用构造函数属性与实例属性的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The startApp function assigns to the constructor (the startApp function) - it's a property of the class, not a property of an instance. If you want to check the PACKAGE and PATH, check the properties of the constructor (either a.constructor or System.startApp):

var System = {

startApp:function(package) {

System.startApp.PACKAGE = package;

System.startApp.PATH = "@APP:/" + package;

},

};

var a = new System.startApp("Mr. T.");

console.log(a.constructor.PACKAGE + ", " + a.constructor.PATH);

Note that the var a will only be tied to anything useful if there are prototype functions on System.startApp, for example:

var System = {

startApp:function(package) {

System.startApp.PACKAGE = package;

System.startApp.PATH = "@APP:/" + package;

},

};

System.startApp.prototype.doSomething = () => {

console.log('doing something');

};

var a = new System.startApp("Mr. T.");

console.log(a.constructor.PACKAGE + ", " + a.constructor.PATH);

a.doSomething();

If you don't have that, it doesn't make sense for startApp to be called as a constructor, nor does it make much sense to assign properties to the function.

If you want every instance to have separate properties, then assign to properties of this:

var System = {

startApp:function(package) {

this.PACKAGE = package;

this.PATH = "@APP:/" + package;

},

};

var a = new System.startApp("Mr. T.");

console.log(a.PACKAGE + ", " + a.PATH);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值