87,903
社区成员
发帖
与我相关
我的任务
分享
<p id='asd'>求助</p>
<a onclick = $('asd').test('123456')><a>
function $(id){
if(typeof id == 'undefined'){
alert('must a string');
}
if(this == window){
return new $(id);
}
this.dom = document.getElementById(id);
}
$.prototype = {
test : function(text){
this.dom.innerText = text;
}
}
但是我的代码里 不返回对象,却不能实现$(id).test(str)这种,只能这样用$.test(str),但是这样就没法用$()获得要响应事件的标签,只能又去写document......了。 总之单独使用$()可以,连用只能$.test()。
(function(window){
_$ = window.$;
$ = function(o){
if(Boolean(o) == false)
return event.srcElement;
else
return typeof(o) == "string"?document.getElementById(o):o;
};
$.test=function(str){
document.write('郁闷蛋疼'+str);
}
}(window))
function $(id){
if(typeof id == 'undefined'){
alert('must a string');
}
if(this == window){
return new $(id);
}
this.dom = document.getElementById(id);
}
$.prototype = {
test : function(text){
this.dom.innerText = text;
}
}
但是我的代码里 不返回对象,却不能实现$(id).test(str)这种,只能这样用$.test(str),但是这样就没法用$()获得要响应事件的标签,只能又去写document......了。 总之单独使用$()可以,连用只能$.test()。
(function(window){
_$ = window.$;
$ = function(o){
if(Boolean(o) == false)
return event.srcElement;
else
return typeof(o) == "string"?document.getElementById(o):o;
};
$.test=function(str){
document.write('郁闷蛋疼'+str);
}
}(window))
[/quote]
LZ要先了解下怎么创建对象,你的这个实例不是对象,你的$和$.test只是两个独立的函数,只不过函数名使用了命名空间而已。
可能大家给的例子有点复杂,看看下面
function Class(id)
{
//对象的构造函数
}
Class.prototype.mt = function ()
{
//对象的一个方法
alert(34);
}
//这个Class对象的使用方法有
//方法1:
var obj = new Class(123);
obj.mt();
//方法2:
new Class(123).mt();
//方法3:
var $ = function (id)
{
return new Class(id);
};
$(123).mt();
方法3就是你想要有吧
[/quote]
这回明白了
非常感谢
Class.prototype 是给class添加属性的吧
class.prototype={
}function $(id){
if(typeof id == 'undefined'){
alert('must a string');
}
if(this == window){
return new $(id);
}
this.dom = document.getElementById(id);
}
$.prototype = {
test : function(text){
this.dom.innerText = text;
}
}
但是我的代码里 不返回对象,却不能实现$(id).test(str)这种,只能这样用$.test(str),但是这样就没法用$()获得要响应事件的标签,只能又去写document......了。 总之单独使用$()可以,连用只能$.test()。
(function(window){
_$ = window.$;
$ = function(o){
if(Boolean(o) == false)
return event.srcElement;
else
return typeof(o) == "string"?document.getElementById(o):o;
};
$.test=function(str){
document.write('郁闷蛋疼'+str);
}
}(window))
[/quote]
LZ要先了解下怎么创建对象,你的这个实例不是对象,你的$和$.test只是两个独立的函数,只不过函数名使用了命名空间而已。
可能大家给的例子有点复杂,看看下面
function Class(id)
{
//对象的构造函数
}
Class.prototype.mt = function ()
{
//对象的一个方法
alert(34);
}
//这个Class对象的使用方法有
//方法1:
var obj = new Class(123);
obj.mt();
//方法2:
new Class(123).mt();
//方法3:
var $ = function (id)
{
return new Class(id);
};
$(123).mt();
方法3就是你想要有吧
var divDrag = {
o: null,
init: function (divHeader, DivContent) {
divHeader.onmousedown = this.start;
DivContent.onmousedown = this.start;
},
start: function (e) {
var o;
divDrag.o = o = document.getElementById("tanchu_warrper");
e = divDrag.fixEvent(e);
e.preventDefault && e.preventDefault();
o.x = e.clientX - divDrag.o.offsetLeft + 239;
o.y = e.clientY - divDrag.o.offsetTop + 150;
document.onmousemove = divDrag.move;
document.onmouseup = divDrag.end;
},
move: function (e) {
e = divDrag.fixEvent(e);
var oLeft, oTop;
oLeft = e.clientX - divDrag.o.x + 239;
oTop = e.clientY - divDrag.o.y + 150;
divDrag.o.style.left = oLeft + 'px';
divDrag.o.style.top = oTop + 'px';
divleft = oLeft * 1;
divtop = oTop * 1;
},
end: function (e) {
e = divDrag.fixEvent(e);
divDrag.o = document.onmousemove = document.onmouseup = null;
},
fixEvent: function (e) {
if (!e) {
e = window.event;
e.target = e.srcElement;
e.layerX = e.offsetX;
e.layerY = e.offsetY;
}
return e;
}
}
divDrag.init(div, divBox);
= -= 像这种 封装方法就可以实现
<p id='asd'>求助</p>
<script>
function $(id){
this.dom = document.getElementById(id)
}
$.prototype = {
changeText : function(){
this.dom.innerText = 'fuck ?';
return this;
},
changeStyle : function(){
this.dom.style.color = 'red';
return this;
}
}
new $('asd').changeText().changeStyle();
</script>
return new $(id);
把自己作为对象返回该如何理解? 在用$().test()的时候 相当于(new $(id)).test()? 然后又返回一个对象(new (new $(id))).test()? 不太明白[/quote]
if在那摆着,哪能让你new两次啊。
伦家$的意思是,你要是调用我的时候,用的是$(id)这种形式,就给你修正一下,论家本来要用new $(id)。不排除某些人很懒,忘记加new。so,给你来了个if,然后给你加上new。如果你就是用的new,那if根本进不去。[/quote]
然后呢? new完的对象返回给谁了? test里面用的是dom属性,根本没用到那个对象阿。return new $(id);
把自己作为对象返回该如何理解? 在用$().test()的时候 相当于(new $(id)).test()? 然后又返回一个对象(new (new $(id))).test()? 不太明白[/quote]
if在那摆着,哪能让你new两次啊。
伦家$的意思是,你要是调用我的时候,用的是$(id)这种形式,就给你修正一下,论家本来要用new $(id)。不排除某些人很懒,忘记加new。so,给你来了个if,然后给你加上new。如果你就是用的new,那if根本进不去。return new $(id);
把自己作为对象返回该如何理解? 在用$().test()的时候 相当于(new $(id)).test()? 然后又返回一个对象(new (new $(id))).test()? 不太明白function $(id){
if(typeof id == 'undefined'){
alert('must a string');
}
return new $.prototype.init(id);
}
$.prototype = {
test : function(text){
this.dom.innerText = text;
},
init : function(id){
this.dom = document.getElementById(id);
}
}
$.prototype.init.prototype = $.prototype;
function $(id){
if(typeof id == 'undefined'){
alert('must a string');
}
if(this == window){
return new $(id);
}
this.dom = document.getElementById(id);
}
$.prototype = {
test : function(text){
this.dom.innerText = text;
}
}
(function(window){
_$ = window.$;
$ = function(o){
if(Boolean(o) == false)
return event.srcElement;
else
return typeof(o) == "string"?document.getElementById(o):o;
};
$.test=function(str){
document.write('郁闷蛋疼'+str);
}
}(window))
上面的$.test()可以执行;但是换成$().test()就不行 ,求解[/quote]
不是说了么。$()函数要返回对象的实例
(function(window){
_$ = window.$;
$ = function(o){
if(Boolean(o) == false)
return event.srcElement;
else
return typeof(o) == "string"?document.getElementById(o):o;
};
$.test=function(str){
document.write('郁闷蛋疼'+str);
}
}(window))
上面的$.test()可以执行;但是换成$().test()就不行 ,求解