JavaScript 轻松实现强大的Cookies操作

刚刚群里有人问到JavaScript的Cookies操作, 于是想到之前也写过一个, 刚刚一搜, 是去年8月份的事情了… 不过功能还算不错, 而且使用方便, 于是整理一下, 献上:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
    <title></title>
    <script src=”http://www.vilic.info/vejis/vejis.1.0.0.js” type=”text/javascript”></script>
    <script src=”http://www.vilic.info/vejis/cookies.1.0.0.js” type=”text/javascript”></script>
    <script type=”text/javascript”>
        /*
        cookies[String name] 获取cookies集合中名为name的Cookie的值.
        cookies.clear() 清除所有Cookie, 并更新cookies集合, 无返回值.
        cookies.del(String name) 删除相应名称的Cookie, 并更新cookies集合, 无返回值.
        cookies.del(Array names) 批量删除相应名称的Cookie, 并更新cookies集合, 无返回值.
        cookies.get(String name) 更新cookies集合, 获取相应名称的Cookie的值, 返回值为String.
        cookies.refresh() 更新cookies集合, 无返回值.
        cookies.set(String name, String value) 设置相应名称的Cookie的值, 并更新cookies集合, 无返回值.
        cookies.set(String name, String value, Date date) 设置相应名称的Cookie的值与过期时间, 并更新cookies集合, 无返回值.
        cookies.set(Array names, Array values) 批量设置相应名称的Cookie的值, 并更新cookies集合, 无返回值.
        */

        if (cookies[“abc”] == undefined) {
            alert(“set cookie”);
            cookies.set(“abc”, “123”);
        }

        alert(cookies[“abc”]);
    </script>
</head>
<body>

</body>
</html>

相关文件:
http://www.vilic.info/vejis/vejis.1.0.0.js
http://www.vilic.info/vejis/cookies.1.0.0.js

Original link of this archive: http://vilic.info/blog/archives/646
本文的原始链接: http://vilic.info/blog/archives/646

JavaScript 实现函数重载及类的多构造函数

这个是之前一篇 (http://www.vilic.info/blog/archives/610) 的升级版, 添加了更强的对 “类” 的支持, 取消了Null类型, 顺便修正了一些bug.

脚本链接: http://www.vilic.info/vejis/vejis-mo.js

然后是用法:

/* 函数的用法 */

//创建一个函数并定义重载1
//这里也可以使用 var fn = _(); 创建空函数, 并在以后添加重载
var fn = _(function () {
    alert(“No arguments!”);
});

//重载2
fn._(String, function (str) {
    alert(“You got me a string: ” + str + “.”);
});

//重载3
fn._(String, Integer, function (str, n) {
    alert(“The string is: ” + str + “; and the integer is: ” + n + “.”);
});

fn();
fn(“test1”);
fn(“test2”, 123);

/* 类的用法 */
//定义一个类
var C = class_(function () {
    var x;

    //成员函数
    this.getX = _(function () {
        return x;
    });

    /* 也可无构造函数 */
    //构造函数重载1
    C._(Integer, function (i) {
        x = “Integer: ” + i;
    });

    //构造函数重载2
    C._(String, function (s) {
        x = “String: ” + s;
    });
});

//创建两个C的实例
var c1 = new C(“abc”);
var c2 = new C(123);

alert(c1.getX());
alert(c2.getX());

Original link of this archive: http://vilic.info/blog/archives/638
本文的原始链接: http://vilic.info/blog/archives/638

Vibot 2011.3.10

I don’t know if this moment should be called too late or too early…

After hours’ work, I have finally made the outline-searching algorithm for a formatted map. I know it cannot be called difficult to many programmers, even not for me. But the feelings are good, cause I don’t write this kind of code usually.

I thought about how to find the right route efficiently for sometime, and came out with this solution. Firstly find the possible part of the whole outline, and… enumerate every point on it… I don’t really like enumeration, cause it always means inefficiency. But sometimes you know, It’s unavoidable.

So, I really need to go to sleep now.

Hope me a nice day after waking up!

Original link of this archive: http://vilic.info/blog/archives/635
本文的原始链接: http://vilic.info/blog/archives/635

Vibot 2011.3.7

I formally started my robot project today. And when I was about to write this post, I got the project a temporary name, Vibot.

I don’t have a very good skill on algorithm, so I am beginning with a relatively simple thing — find an available route on a formatted map. I have thought for a while, and come out with a seemly simple solution. I’ve no idea if it will be easy to make it via CODE…

And by the way, I really think the most useful skill on mathematic for these kinds of algorithms is Geometry.

Best wishes to myself!

Original link of this archive: http://vilic.info/blog/archives/629
本文的原始链接: http://vilic.info/blog/archives/629