扫码关注官方订阅号
javascript中的uber怎么用求解释
认证0级讲师
uber 只是一个对象属性而已,没有什么特殊的。然后虽然看着哟亣多,但其实这就是一个递归而已。我们先把 toString 的代码抄一下:
uber
toString
Shape.prototype.toString = function() { var result = []; if( this.constructor.uber ) { result[ result.length ] = this.constructor.uber.toString(); } result[ result.length ] = this.name; return result.join(', '); }
第一次 my.toString ,此时
my.toString
this.constructor.uber.toString = Triangle.uber.toString = TwoDShape.prototype.toString
这时候调用的就是 TwoDShape.toString 了,同样再走一遍流程,此时
TwoDShape.toString
this.constructor.uber.toString = TwoDShape.uber.toString = Shape.prototype.toString
这时候调用的就是 Shape.toString 了,因为 Shape.uber 不存在,所以 if 就直接跳过了,执行后面的 result[ reult.length ] = this.name 并返回结果,也就是 shape。
Shape.toString
Shape.uber
if
result[ reult.length ] = this.name
shape
跳回 TwoDShape.toString ,此时条件语句就定义了 result[0] = 'shape',然后后面就定义了 result[1] = '2D shape',所以 TwoShape.toString 返回的就是 shape, 2D shape。
result[0] = 'shape'
result[1] = '2D shape'
TwoShape.toString
shape, 2D shape
跳回 Triangle.toString,根据上一条我们知道条件语句返回的就是 result[0] = 'shape, 2D shape',然后后面定义了 result[1] = 'Triangle',所以 my.toString 返回的就是 shape, 2D shape, Triangle。
Triangle.toString
result[0] = 'shape, 2D shape'
result[1] = 'Triangle'
shape, 2D shape, Triangle
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
uber只是一个对象属性而已,没有什么特殊的。然后虽然看着哟亣多,但其实这就是一个递归而已。我们先把toString的代码抄一下:第一次
my.toString,此时这时候调用的就是
TwoDShape.toString了,同样再走一遍流程,此时这时候调用的就是
Shape.toString了,因为Shape.uber不存在,所以if就直接跳过了,执行后面的result[ reult.length ] = this.name并返回结果,也就是shape。跳回
TwoDShape.toString,此时条件语句就定义了result[0] = 'shape',然后后面就定义了result[1] = '2D shape',所以TwoShape.toString返回的就是shape, 2D shape。跳回
Triangle.toString,根据上一条我们知道条件语句返回的就是result[0] = 'shape, 2D shape',然后后面定义了result[1] = 'Triangle',所以my.toString返回的就是shape, 2D shape, Triangle。