python给js变量赋值_JS仿Python的from …… import ……方法将序列化对象的元素转换为(全局)变量...

//JS仿Python的from …… import ……方法将序列化对象的元素转换为(全局)变量

//也跟PHP数组中的list()方法功能相似

//没事写的玩,也许没有什么实际应用价值

var $object = {};

$object.from = function(_class) {

//if(_class && typeof (_class == "object")) {

this.class = _class;

return this;

//}

}

$object.import = function() {

for (var i=0; i

if(window[arguments[i]]) delete window[arguments[i]];

//alert(arguments[i]+"=>"+this.class[arguments[i]]);

window[arguments[i]] = this.class[arguments[i]];

}

}

//测试一:序列化对象

var test = {

name : "pandao",

work : "Web Designer",

fn : function(str) {

alert(str);

}

};

//$object.from(test).import("name", "work", "fn");

//alert("name=>"+name+", work=>"+work);

//fn("speech");

//测试二:数组对象

var test2 = [];

test2['name'] = "Tome";

test2['work'] = "Programmer";

test2['fn'] = function(str) {

alert(str);

};

//$object.from(test2).import("name", "work", "fn");

//alert("name=>"+name+", work=>"+work);

//fn("run");

//测试三:JS类对象

function testClass() {

this.name = "testClass";

this.version = "V1.0";

}

//公共静态方法

testClass.getVersion = function() {

alert(this.version);

return this;

};

//公共方法

testClass.prototype = {

add : function() {

alert("testClass.add()");

return this;

},

remove : function() {

alert("testClass.remove()");

return this;

}

//getVersion : function() {

//alert("version=>"+this.version);

//return this;

//}

};

var test3 = new testClass();

$object.from(test3).import("name", "version", "add", "remove", "getVersion");

alert("name=>"+name+", version=>"+version);

add();

remove();

getVersion(); //不这样调用公共静态方法,会出错了,暂时没想到解决方法,求教,错误信息:Uncaught TypeError: Property 'getVersion' of object [object Window] is not a function

后话:

PHP.JS 是一个开源的JavaScript 库,它尝试在JavaScript 中实现PHP 函数,它有实现PHP的list()方法。http://phpjs.org/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值