Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Проблема с return this (https://javascript.ru/forum/misc/26418-problema-s-return.html)

(Sandr) 07.03.2012 21:26

Проблема с return this
 
Пытаюсь построить конструкцию подобную jQuery. Но возникли проблемы..



function $(data) { return new func(data); }
function func(data) {
	this.obj = document.getElementById(data);
	return this;
}

func.prototype.drag = function(data) {
	var drag = this.obj;
	drag.onmousedown = function(e) {
		var self = this.obj;
		e = $().getEvent(e);
		drag.style.position = 'absolute';
		var coords = $().getCoords(self);
		var shiftX = e.pageX - coords.left;
		var shiftY = e.pageY - coords.top;
		document.onmousemove = function(e) {
			drag.style.left = e.pageX - shiftX + 'px';
			drag.style.top = e.pageY - shiftY + 'px';
		};
		document.onmouseup = function() {
			drag.style.cursor = 'default';
			document.onmousemove = document.onmouseup = document.ondragstart = document.body.onselectstart = null;
		};
	};
	drag.ondragstart = function() { return false; };
}


Обращаюсь так:
$('el').drag();
Всё работает, но мне хотелось бы сделать так, чтобы $('el') возвращало объект этого элемента. Пишу так:
function func(data) {
	this.obj = document.getElementById(data);
	return this.obj;
}


И после этого идёт полный фэйл(( К примеру, лиса негодует и визжит: " mj("el").drag is not a function ".

Ребят, как исправить проблему?

(Sandr) 08.03.2012 23:22

nasqad,
нее. Это не то.
//ну да ладно, задачу решил обходными путями))


Часовой пояс GMT +3, время: 13:05.