`
站在岸上的鱼
  • 浏览: 824 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
悟透JavaScript js, javascript
function Class(){
	var aDefine = arguments[arguments.length-1];//最后一个参数为类定义
	if (!aDefine)//没有参数就返回
	{
		return;
	}
	var aBase = arguments.length > 1?arguments[0]:object;//解析基类
	function prototype_(){};//构造prototype的临时函数,用于挂接原型链
	prototype_.prototype = aBase.prototype;//准备传递prototype
	var aPrototype = new prototype_();//建立类要用的prototype

	for (var member in aDefine)//复制类定义到当前类的prototype
	{
		if (member != "Create")//构造函数不用复制
		{
			aPrototype[member] = aDefine[member];
		}
	}
	//如果有构造函数
	if (aDefine.Create)
	{
		var aType = aDefine.Create;//类型即为该构造函数
	}
	else{//否则为默认构造函数。
		aType = function(){
			this.base.apply(this,arguments);
		}
	}

	aType.prototype = aPrototype;		//设置类(构造函数)的prototype
	aType.Base = aBase;					//设置类型关系
	aType.prototype.Type = aType;		//为本类对象扩展一个Type属性
	return aType;						//返回构造函数作为类
}

//根类object定义
function object(){};//定义小写的object根类,用于实现最基础的方法等
object.prototype.isA = function(aType){//判断是否属于某类型
	var self = this.Type;
	while (self)
	{
		if(self == aType) return true;
		self = self.Base;
	}
	return false;
}
//调用基类构造函数
object.prototype.base = function(){
	var Caller = object.prototype.base.caller;
	Caller && Caller.Base && Caller.Base.apply(this,arguments);
}

//应用效果
//默认派生自object基本类
var Person = Class({
	Create:function(name,age){
		this.base();
		this.name = name;
		this.age = age;
	},
	sayHello:function(){
		alert("Hi,I am " + this.name + ", "  + this.age + " years old.");
	}
});
//Employee派生自Person
var Employee = Class(Person,{
	Create:function(name,age,salary){
		this.base(name,age);//调用基类的构造函数
		this.salary = salary;
	},
	showMeTheMoney:function(){
		alert(this.name + "'s salary is: $" + this.salary);
	}
});

var wml = new Person("Wu Mingli",29);
//wml.sayHello();
//var xyd = new Employee("Xu yedong",28,7500);
//xyd.showMeTheMoney();
var lyp = new wml.Type("Lv yaping",25);
lyp.sayHello();
测试微博API引用页面 weibo
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试</title>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> 
</head>
<body>
<div id="weibocon"></div>
<script src="weiboapi.js"></script>
<script>
window.onload = weibo;
var wb = new weibo();
wb.init(
	null,
	"img",//参数说明:text-文本微博;img-选择图片发微博;trend-获取话题列表
	{
		topic:encodeURI("装修"),//当第2个参数为trend时起作用
		count:10,
		length:30,
		zturl:document.location.href//也可以指定专题链接,如:http://jiaju.sina.com.cn/news/zt/zturl.html
	}
);
wb.showmsg();
</script>
</body>
</html>
测试微博API
/*
*author:武明礼
*for:test
*developed at :2011.8.29
*Copyright 2011. All rights reserved.
*/
 
document.write('<script type="text/javascript" src="jquery-1.6.1.min.js"><\/scr'+'ipt>');//引入jQuery
document.write('<script type="text/javascript" src="http://i.sso.sina.com.cn/js/sinaSSOManager.js"></scr'+'ipt>');//引入SSO
//document.location.host = 'sina.com.cn';
function weibo(){};
weibo.prototype = {
	init:function(action,type,style){
		switch(type){
			case "text":
				var zturl = style.zturl;
				initText(zturl);
				break;
			case "img":
				initImgWeibo();
				break;
			case "trend":
				var con = style.topic;
				var ct = style.count;
				var len = style.length;
				trend(con,ct,len);
				break;
		}
	},
	uinfo:function(){
		return sinaSSOManager.getSinaCookie();
		/*
		return data:
		ag		"8"
		dob		"1982-09-01"
		email	"mingli@sina.cn"
		nick	"站在岸上的鱼"
		sex		"1"
		ssl		"0"
		uid		"1627910305"
		user	"mingli.cn"
		*/
	}
}
weibo.prototype.showmsg = function(){
	alert(this.unifo);
}
 
//话题
function trend(content,count,len){
	var count = count || 10;
	var style = '<style>#trendlist li {font-size:12px;height:25px; line-height:25px;}<\/style>';
	document.write(style);
	var html = '<ul id="trendlist" style="font-size:12px;">';
	var url = "http://supports.jiaju.sina.com.cn/api/weibo.php?action=trend&trend="+content+"&page=1&count=" +count+ "&callback=?";
	$.getJSON(url,
	function(data) {
		var dt = data.statuses;
		for (var i=0; i < dt.length;i++ )
		{
			document.write('<li style="height:25px; line-height:25px;font-size:12px;">'+dt[i].text.substr(0,len)+'</li>');
		}
    });
	html += "</ul>";
	document.write(html);
}
 
//初始化纯文本微博
function initText(url){
	var html = '<div id="textweibo"><textarea name="content" id="content" rows="5" cols="30"></textarea><br /><input type="submit" id="fbbtn" value="发布"><br /><br /></div>';
	document.write(html);
	$('#fbbtn').bind("click",function(){
		var text = $("#content").val();
		send(1,null,encodeURI(text),url);
	});
}
//初始化图片主传类微博
function initImgWeibo(){
	var html = '<form name="form" method="post" enctype="multipart/form-data" action="http://supports.jiaju.sina.com.cn/api/weibo.php?action=send&file_tag_name=upfile&type=3"><textarea name="content" id="content" rows="5" cols="30"></textarea><br /><input type="file" id="upfile" name="upfile"> <input type="submit" value="提交"></form>';
	document.write(html);
}
 
//发布微博
function send(type,pic,content,url){
	var link='';
	if(type == 1 && pic == null){
		link="http://supports.jiaju.sina.com.cn/api/weibo.php?action=send&type="+type+"&content="+content+" "+url+"&callback=?";
	}
	else if(type == 2 && pic!=''){
		link="http://supports.jiaju.sina.com.cn/api/weibo.php?action=send&type="+type+"&content="+content+" "+url+"&url="+pic+"&callback=?";
	}
 
    $.getJSON(link,
    function(data) {
		if(data=='-1'){
		  alert('未登录,请先登录');
		 }else if(data=='-2'){
		   alert('您还没有开通微博!');
		 }else if(data.error_code=='20019'){
		   alert('内容重复,不要太贪心哦~');
		 }else if(data.error_code=='10016'){
		   alert('内容为空,禁止发布!');
		 }else if(data.mid>0){
		   alert('发布成功');
		 }else{
			alert('发布失败:Error info:'+data.error);
		 }
    });
}
//转发
function transmit(){
	var content=$("#content").val();
	var url ="http://supports.jiaju.sina.com.cn/api/weibo.php?action=transmit&content="+content+"&wid=3362282635551012&callback=?";
	$.getJSON(url,
    function(data) {
		alert(data.id);
    });
}
//评论
function comment(){
	var content=$("#content").val();
	var url ="http://supports.jiaju.sina.com.cn/api/weibo.php?action=comment&content="+content+"&wid=3362282635551012&callback=?";
	$.getJSON(url,
    function(data) {
		alert(data.id);
    });
}
//关注
function follow(uid){
	var url ="http://supports.jiaju.sina.com.cn/api/weibo.php?action=followme&uid="+uid+"&callback=?";
	$.getJSON(url,
    function(data) {
		if (data.id || data.error_code == 20506)
		{
			alert("已关注");
		}
    });
}
Global site tag (gtag.js) - Google Analytics