Category Archives: WebTech - Page 3

Get the real type in javascript

In javascript, we can use “type” function to get the type of an element (e.g “aa”, [], {}, 1, true, null, undefined, function, global), but get an array’s type. it returns a “object”. So, how to get the real type of the element?

USE “Object.prototype.toString.apply” function.

WHY?

Because any elements inherit it. Now, let’s see the results

Object.prototype.toString.apply("aa") //return [object String]
Object.prototype.toString.apply(1) //return [object Number]
Object.prototype.toString.apply({}) //return [object Object]
Object.prototype.toString.apply([]) //return [object Array]
Object.prototype.toString.apply(true) //return [object Boolean]
Object.prototype.toString.apply(null) //return [object Null]
Object.prototype.toString.apply(undefined) //return [object Undefined]
Object.prototype.toString.apply(window) //return [object global]
Object.prototype.toString.apply(Math.abs)//return [object Function]

//... and all

[php]unpack “N” bug on 64 bit system

这个问题只有在64系统下有问题. 当一个数值为负数时候, unpack后会变成一个超大整数.这是由于PHP_INT_MAX导致的.
32为下的PHP_INT_MAX为 2147483647; 而64位下为9223372036854775807;
如何判断php是运行在64位下?

php -r "echo PHP_INT_SIZE;"

如何返回4为32bit, 8为64bit.

我们看下例子

<?php
$s = -379;
$a = pack("N", $s);//转化后还是正常的.得到 ff ff fe 85

print_r(unpack("N", $a));//在32为解出来的结果正常为-379. 而64bit下则为4294966917
?>

如何解决?
网上找到了这么一个处理办法

/// portably build 64bit id from 32bit hi and lo parts
function _Make64 ( $hi, $lo )
{
// on x64, we can just use int
if ( ((int)4294967296)!=0 )
return (((int)$hi)<<32) + ((int)$lo);// workaround signed/unsigned braindamage on x32 $hi = sprintf ( "%u", $hi ); $lo = sprintf ( "%u", $lo );// use GMP or bcmath if possible if ( function_exists("gmp_mul") ) return gmp_strval ( gmp_add ( gmp_mul ( $hi, "4294967296" ), $lo ) );if ( function_exists("bcmul") ) return bcadd ( bcmul ( $hi, "4294967296" ), $lo );// compute everything manually $a = substr ( $hi, 0, -5 ); $b = substr ( $hi, -5 ); $ac = $a*42949; // hope that float precision is enough $bd = $b*67296; $adbc = $a*67296+$b*42949; $r4 = substr ( $bd, -5 ) + + substr ( $lo, -5 ); $r3 = substr ( $bd, 0, -5 ) + substr ( $adbc, -5 ) + substr ( $lo, 0, -5 ); $r2 = substr ( $adbc, 0, -5 ) + substr ( $ac, -5 ); $r1 = substr ( $ac, 0, -5 ); while ( $r4>100000 ) { $r4-=100000; $r3++; }
while ( $r3>100000 ) { $r3-=100000; $r2++; }
while ( $r2>100000 ) { $r2-=100000; $r1++; }

$r = sprintf ( “%d%05d%05d%05d”, $r1, $r2, $r3, $r4 );
$l = strlen($r);
$i = 0;
while ( $r[$i]==”0″ && $i<$l-1 ) $i++; return substr ( $r, $i ); }list(,$a) = unpack ( "N", "\xff\xff\xff\xff" ); list(,$b) = unpack ( "N", "\xff\xff\xff\xff" ); $q = _Make64($a,$b); var_dump($q); [/php]

[PHP]如何判断array真正的类型

习惯了javascript中的array和object.在php中array又可以是Array也可以是Object(这里的Object 与js的object相同. 并非stdClass的Object). 但是问题来了如何判断他真正的类型.在有些时候, 我们需要它真实的类型.才能做一点事情.
目前我找到一个非常挫的方法.通过判断array头部的key是否为0, array最后一个元素的key是否为count – 1值. 并且同时判断, 他的所有key是否为数字. 缺陷在于如果加入一个数组为array(1, 2 => “c”, 10 => 6, 4=>”1″, 3). 这个时候仍然能通过.
刚刚测试了一下, 如何改动了key值. 后面会根据上面一个key值变化.测试如下:

Array ( [0] => 1 [2] => c [10] => 6 [4] => 5 [11] => 3 )

判断方法: 如果有更好的, 请留贴告诉我.

function getRealArrayType($d){
		if (gettype($d) != "array"){return;}	
		//get realtype
		$realType = "array";
		//array / object
		$count = count($d);
		reset($d);
		//first element key is 0?
		if (key($d) != 0){
			$realType = "object";
		}
		end($d);
                //the last element key is (count - 1)
		if (key($d) != ($count - 1)){
			$realType = "object";
		}

		reset($d);
		while (next($d)){
			$key = key($d);
			if (is_string($key)){
				$realType = "object";
				break;
			}
		}
		reset($d);//reset position
		return $realType;
	}

[RT] Defining Getters and Setters in NodeJS

Recently I discovered something interesting while messing around with NodeJS… you can define getters and setters using the ECMAScript5 syntax. For a useless example I could do something like this:

var sys = require('sys')

function Person(age){
this.__defineGetter__('age', function(){
return age
})

this.__defineSetter__('age', function(arg){
age = arg
})
}

var dude = new Person(18)

sys.puts(dude.age)
dude.age = 32

sys.puts(dude.age)

This is kind of boring though because it’s just a more complicated way of doing

function Person(age){
this.age = age
}

however you can notice in the first example you can add any kind of custom behavior to the getter and setter and access it via person.age. Where this does become useful however is if you want to create a read only property, which you can do by just defining the getter alone:

var sys = require('sys')

function Person(age){
this.__defineGetter__('age', function(){
return age
})
}

var dude = new Person(18)

sys.puts(dude.age)
dude.age = 32

This will print the initial age as expected, however the last line where the attempt is made to set the property will throw the exception “TypeError: Cannot set property age of # which has only a getter.”

A small discovery, yet very useful.

This origin post url: http://blog.james-carr.org/2010/07/19/defining-getters-and-setters-in-nodejs/

Ext4中DataStore以及Model需要注意的几点

最近使用ExtJS在做项目, 其中grid会大量的使用Model以及Data.Store.平常使用是没有问题的, 如果做高级搜索的时候, 按照一般情况下, 当然时重建model以及data.store, 就是按以上的思路来做的, 起初的几次, 也没什么问题. 但是使用多次高级搜索之后, 就会报一个this model is undefined错误.
遍历整个代码, model每次都会赋值, 为什么还会是undefined? Google搜索之, 也没找到任何原因.
今天, 重新看Ext文档的时候, 发现了Data.Store的setProxy方法, proxy是用于将搜索条件以及调用函数储存起来, 然后extjs内部会去读取这个object.生成你想要的数据.
目前我已经高级搜索的方法都改成store.setProxy(object); store.loadPage(1); 目前用户测试反馈下来, 无报错..

Geolocation and get the address by Google Map API

在移动设备以及现代浏览器中可以直接同navigator.geolocation所提供的api, 获取你当前经纬度. 并且通过GoogleMapAPI, 查看你当前所在位置的地图.

一. 在客户端上获取经纬度.

获取经纬度有两个api: getCurrentPosition以及watchPosition. 从本质上来说都是获取你当前所在的位置.

getCurrentPosition(callback)

watchPosition(successCallback, failureCallback, options)

在stackoverflow上找到了一篇讨论两者不同的论题. http://stackoverflow.com/questions/1948952/watchposition-vs-getcurrentposition-w-settimeout

After some serious testing, I have verified watchPosition() will give you an accurate location much more quickly than getCurrentPostion() over and over again. When using watchPostion(), the map behaves poorly if you redraw it over and over again every time the device updates your location. To get around this, I have added a listener to the tilesloaded event, which allows me to only redraw the map if there is not already a thread trying to draw on the map. Once the user is happy with the determined location, I will clear the watch. This will get me the best of both worlds, as far as battery consumption and accuracy are concerned.

这个就看你个人喜欢用哪个api了, 我推荐后者watchPosition. 首先写一个页面用来获取经纬度
Read more »

初次使用NodeJS构建RESTful服务

最近要给项目组用NodeJS写一个简单的RESTful. 在网上简单查了些资料。首先需要在服务器上安装NodeJS以及npm(NodeJS包管理工具). 这里不在重复安装的过程.

安装好这些, 然后需要安装webservice, mysql等模块.

npm install webservice mysql

安装完成之后. 创建一个目录demo, 并且创建一个server.js以及demo.js

Read more »