我听说你可以将数字右移一个.5而不是使用Math.floor().我决定检查其限制以确保它是合适的替代品,因此我检查了以下值并在Google Chrome中获得了以下结果:
2.5 >> .5 == 2;
2.9999 >> .5 == 2;
2.999999999999999 >> .5 == 2; // 15 9s
2.9999999999999999 >> .5 == 3; // 16 9s
Run Code Online (Sandbox Code Playgroud)
在一些摆弄之后,我发现两个最高可能的值,当右移0.5时,将产生2,在Chrome和Firefox中是2.999999999999997779553950949686919152736663818359374999999(以及9重复).IE中的数字为2.9999999999999997779.
我的问题是:数字的意义是什么.0000000000000007779553950749686919152736663818359374?这是一个非常奇怪的数字,它真的激起了我的好奇心.
我一直试图找到答案或至少某种模式,但我认为我的问题在于我真的不理解按位操作.我原则上理解这个想法,但是将一个序列移动到.5对我来说根本没有任何意义.任何帮助表示赞赏.
为了记录,奇怪的数字序列以2 ^ x变化.以下数字的最高可能值仍然正确截断:
for 0: 0.9999999999999999444888487687421729788184165954589843749¯ for 1: 1.9999999999999999888977697537484345957636833190917968749¯ for 2-3: x+.99999999999999977795539507496869191527366638183593749¯ for 4-7: x+.9999999999999995559107901499373838305473327636718749¯ for 8-15: x+.999999999999999111821580299874767661094665527343749¯ ...and so forth
人们在他们的Greasemonkey脚本中使用了哪些鲜为人知但有用的功能和技术?
(请注意,每个答案只有一个功能.)
类似主题:
我是一个后端程序员,我想知道一件事.我只知道一点Javascript.当我查看一些Javascript代码时,我知道基础知识,我理解代码正在尝试做什么.但我从未用它来制作动画.
然后我听说了jQuery.在不了解Javascript的情况下,我可以成为jQuery的专家吗?
Ajax怎么样?我可以使用jQuery进行Ajax动画(搜索建议),而不必学习Javascript吗?
我是Javascript的新手.我理解匿名函数的概念 - 但是闭包似乎不太清楚.两者之间的相似性(至少在我看来)是令人困惑的.
有人可以解释这个区别吗?(最好是使用一些代码片段来说明要点更清楚).
当点击类numObj的元素时,我想显示一个文本框.出于某种原因,我得到了NaNNaNaNaNNaNNaNaNaN,我希望在下面的代码中看到searchForm变量的结果.
我知道NaN代表的不是数字.我不明白为什么Javascript期待一个数字?我无法理解它为何如此关心.
$(".numObj").live('click',function(){
var preId = $(this).attr('preId');
var arrayPos = $(this).attr('numArrayPos');
alert(preId +" "+arrayPos);
var searchForm = "<table border='0' cellspacing='0' cellpadding='4' id='add-tag2'>"+
+"<tr class='normal'><td bgcolor='#EEEEEE' valign='bottom' nowrap='nowrap'><span class='normal-small'>"+
+"<input name='predicate-name2' type='text' class='normal' id='predicate-name2' size='4' />"+
+"</span></td>"+
+"<td bgcolor='#EEEEEE' valign='bottom' nowrap='nowrap'><span class='normal-small'><=</span></td>"+
+"<td bgcolor='#EEEEEE' valign='bottom' nowrap='nowrap'>x</td>"+
+"<td valign='bottom' bgcolor='#EEEEEE'><=</td>"+
+"<td valign='bottom' bgcolor='#EEEEEE'><span class='normal-small'>"+
+"<input type='text' name='object-object2' id='object-object2' class='normal' size='4' />"+
+"</span></td>"+
+"</tr>"+
+"</table>";
$(".numObj").filter("[preId='"+preId+"']").filter("[numArrayPos='"+arrayPos+"']").html(searchForm);
});
Run Code Online (Sandbox Code Playgroud)
生成的numObj类代码看起来像这样
<td><div class="numObj" preid="73" numarraypos="5"><span class="normal">585.0</span></div></td>
Run Code Online (Sandbox Code Playgroud) 我在Firefox上打开indexedDB数据库时遇到错误.此代码在Chrome中运行良好,但在Firefox中失败.
<script type="text/javascript">
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
var request = indexedDB.open("TestDB", 1);
</script>
Run Code Online (Sandbox Code Playgroud)
行上发生Javascript错误indexedDB.open("TestDB", 1).
假设我定义了一个这样的变量
var today = Date();
console.log(today.getMonth()); // Throw Error
Run Code Online (Sandbox Code Playgroud)
而其他类如Error类在没有new运算符的情况下调用它们的方法.
function factorial(x) {
if(x <= 1)
throw Error("x must not be negative");
return x*factorial(x-1);
}
Run Code Online (Sandbox Code Playgroud)
包装器对象(数字,布尔值,字符串)也可以在没有new运算符的情况下调用它们的方法.那么,在调用它们的方法之前,这是唯一需要新操作符或任何对象创建技术的类.
编辑:由于Date()是一个字符串类型,因此应该在不创建对象的情况下调用它们的方法.因为字符串类型的行为就像它们是对象一样.那为什么不呢?
编辑2:我认为这是唯一的核心功能,不能new Date()像其他功能(Array(), String(), Error()等)一样.所以,它也是这种语言或ECMAScript错误的隐藏功能.
在PHP中至少在我的实例中,使用魔术方法是很常见的 - 至少在定义核心类时,大多数其他内容都将从中扩展.
php中的魔术方法以常规方法的规范以特殊方式运行.例如,我书中最常用的方法之一就是__construct()
每次加载一个类时都会执行该构造.因此,例如,如果您希望您的类自我介绍,您可能会执行以下操作:
<?php
class Person
{
function __construct($name)
{
$this->name = $name;
$this->introduceYourself();
}
public function introduceYourself()
{
//if $this->name then echo $this->name else echo i dont have name
echo $this->name ? "hi my name is " . $this->name : "error i dont have name";
}
}
$dave = new Person('dave');
Run Code Online (Sandbox Code Playgroud)
通常,您不会将某些内容传递给构造.
我经常遇到的其他一些包括:
__call()允许您更改调用方法的默认方式.一个很好的例子是覆盖,它允许您在使用任何以单词get开头的方法时获取属性值,或者在方法调用以单词set开始时设置属性值.
__get()用作类属性的重载,我不使用,但有人可能会感兴趣.
__set()用作类属性的重载,我不使用,但有人可能会感兴趣.
__destruct()我也不使用,只要没有对特定对象的其他引用,或在关闭序列期间以任何顺序调用.
问题
在javascript里面有这样的神奇方法吗?
有新的javascript程序员应该知道的任何隐藏的宝石,就像我上面描述的PHP吗?
javascript ×10
jquery ×2
ajax ×1
animation ×1
chaining ×1
constructor ×1
date ×1
greasemonkey ×1
html5 ×1
indexeddb ×1
nan ×1
node.js ×1
php ×1
precision ×1