JavaScript语言实现类似这样的效果: 3.grams.flour

JavaScript语言实现类似这样的效果: 3.grams.flour

参考 Javascript: The Good Parts,第33页

代码如下:
<html>
<body>

<script type="text/javascript">

// copied from page 33 of Javascript: The Good Parts
Function.prototype.method = function (name, func)
{
this.prototype[name] = func;
return this;
}

Number.method('chi', function() {
return this*0.333;
});

document.writeln( (3).chi());
document.writeln( "<br>" );
document.writeln( (3+1).chi() );
document.writeln( "<br>" );
document.writeln( 3.2.chi() );
document.writeln( "<br>" );
document.writeln( 3.0.chi() );
// document.writeln( 3.chi() );
// SyntaxError: identifier starts immediately after numeric literal

</script>

</body>
</html>

运行结果如下:

0.9990000000000001
1.332
1.0656

0.9990000000000001

努力学习,苦中作乐。

Leave a Reply

Your email address will not be published. Required fields are marked *