Цитата:
Цитата:
|
x-yuri,
понял, значит моя конструкция: if(!selText) return; будет работать правильно. |
А как из метода одного объекта, вызвать другой метод этого же объекта?
function QuickQuote(){
document.write('<div onmousedown="quickQuote.showSelText()" class="button" id="divQuickQuote" style="z-index:1000;cursor:pointer;position:absolute;visibility:hidden"><b>Цитировать</b></div>');
var selText = '';
this.GetSelText = function(){
selText = '';
if (window.getSelection && !window.opera) selText = window.getSelection();
else if (document.getSelection) selText = document.getSelection();
else if (document.selection) selText = document.selection.createRange().text;
selText.toString().replace(/(\r?\n\s*){2,}/gi,'\r\n').replace(/^\s+|\s+$/gi,'').replace(/(\ |\t)+/gi,' ');
if (!selText) selText = '<error>';
this.ShowSelText;
};
this.ShowSelText = function(){
alert(selText);
};
}
var oQQ = new QuickQuote();
window.onload = function(){
document.onkeyup = oQQ.GetSelText;
document.onmouseup = oQQ.GetSelText;
}
почему не срабатывает и как сделать, что бы работало? this.ShowSelText; |
если ты пишешь
function o() {
this.method = function() {}
}
для каждого объекта будет создан свой "экземпляр" метода. Лучше
function o() {}
o.prototype.method = function() {}
this.ShowSelText - ссылка на метод, чтобы его вызвать нужно добавить () |
Цитата:
Цитата:
this.ShowSelText(); все равно "ошибка на странице" и никакого alert-а ... У меня происходит вызов: document.onmouseup = oQQ.GetSelText; выделенный текст записывается в selText для объекта oQQ. Вот только низ метода document.onmouseup = oQQ.GetSelText; не получается вызвать метод this.ShowSelText ... |
Цитата:
Цитата:
|
Цитата:
Цитата:
Цитата:
я же не стал бы писать, если б все понимал )) если использовать: this.ShowSelText(); ошибка: this.ShowSelText is not a function ну а если так: this.ShowSelText то и ошибок нет и alert-а нет ... тогда так:
function QuickQuote(){
document.write('<div onmousedown="quickQuote.showSelText()" class="button" id="divQuickQuote" style="z-index:1000;cursor:pointer;position:absolute;visibility:hidden"><b>Цитировать</b></div>');
var selText = '';
}
QuickQuote.prototype.GetSelText = function(){
selText = '';
if (window.getSelection && !window.opera) selText = window.getSelection();
else if (document.getSelection) selText = document.getSelection();
else if (document.selection) selText = document.selection.createRange().text;
selText.toString().replace(/(\r?\n\s*){2,}/gi,'\r\n').replace(/^\s+|\s+$/gi,'').replace(/(\ |\t)+/gi,' ');
if (!selText) return;
this.ShowSelText();
}
QuickQuote.prototype.ShowSelText = function(){
alert('sdf');
}
var oQQ = new QuickQuote();
window.onload = function(){
document.onkeyup = oQQ.GetSelText;
document.onmouseup = oQQ.GetSelText;
}
все равно ошибка: Цитата:
|
в общем, без prototype-ов, так и не понял как добиться вызова одного метода из другого метода, одного и того же объекта. А с prototype-ом, вариант такой:
function QuickQuote(e){
document.write('<div onmousedown="QuickQuote.showSelText()" class="button" id="divQuickQuote" style="z-index:1000;cursor:pointer;position:absolute;visibility:hidden"><b>Цитировать</b></div>');
this.SelText = '1';
}
QuickQuote.prototype.GetSelText = function(){
SelText = '';
if (window.getSelection && !window.opera) SelText = window.getSelection();
else if (document.getSelection) SelText = document.getSelection();
else if (document.selection) SelText = document.selection.createRange().text;
SelText.toString().replace(/(\r?\n\s*){2,}/gi,'\r\n').replace(/^\s+|\s+$/gi,'').replace(/(\ |\t)+/gi,' ');
if (!SelText || SelText == '') return;
QuickQuote.prototype.ShowSelText();
}
QuickQuote.prototype.ShowSelText = function(){
alert(SelText);
}
var oQQ = new QuickQuote();
window.onload = function(){
document.onkeyup = oQQ.ShowSelText;
document.onmouseup = oQQ.GetSelText;
}
|
Цитата:
Цитата:
function c() {}
c.prototype.m1 = function() { alert(1); }
var o = new c();
o.m2 = function() { alert(2); }
o.m1();
o.m2();
Цитата:
Цитата:
function c() {}
c.prototype.m1 = function() {
alert(this instanceof c);
if( this.m2 )
this.m2();
}
c.prototype.m2 = function() { alert(2); }
var o = new c(); // здесь содержимое c.prototype
// копируется в прототип объекта o
var o2 = {
m3: o.m1, // эта ссылка указывает на o.prototype.m1
// (в объекте o нету метода m1
// но он есть в прототипе)
m4: function() {
// вызываем функцию o.m1 так,
// чтобы this указывал на o
o.m1.call( o );
}};
o2.m3(); // при вызове this будет указывать на o2
// а в объекте o2 нету метода m2
o2.m4();
|
x-yuri,
...блин......пойду рисовать схемы. Понимать то понимаю, но вот как обычно, в сознании, структура так и не прояснилась. Че то не хватает....) Благодарю. БЛИн )) но его доканаяю.....на функциях то просто все... |
| Часовой пояс GMT +3, время: 07:58. |