Hi,
I’m trying to write some OO javascript and I’m running into a snag.
I have a private function that is trying to call a privileged function and it’s acting as though the private function can’t see the privileged one. Is this a rule that I just don’t know about?
So, for example:
[code]
var updatePage = function(xmlDocument, startPos, container){
if (xmlDocument != null){
processListings(xmlDocument, container);
}
for (var i = startPos; i < getNumResults(); i++){
addToListingsDiv(container[i]);
}
getPrevListings(false);
getNextListings(false);
}
this.getPrevListings = function(updatePage){
//Code
}
Should I be doing this a different way?
Thanks,
Chris
<i>
</i>var myFunction = function(){
}
<i>
</i>this.myFunction = function(){
}
<i>
</i>this.prototype.myFunction = function(){
}
<i>
</i>getPrevListings(false);
getNextListings(false);
Definitions:
this.getPreviousListings(updatePage){
//Code here
}
&
this.getNextListings(updatePage){
//Code here
}
<i>
</i> getPrevListings(false);
<i>
</i> this.getPrevListings(false);
[code=php]function UpdatePage(xmlDocument, startPos, container) {
// Private
var addToListings = function(c) {
// Code here
};
// Public
this.getNextListings = function(x) {
// Code here
};
// Public
this.getPrevListings = function(x) {
// Code here
};
// Begin constructor code
if (xmlDocument != null){
processListings(xmlDocument, container);
}
for (var i = startPos; i < getNumResults(); i++){
addToListingsDiv(container[i]);
}
this.getPrevListings(false);
this.getNextListings(false);
// End constructor code
}
UpdatePage.prototype = {
// Shared public methods and properties
};[/code]
Maybe I'm misunderstanding privileged vs private vs public.
The way I understand it is as follows:
Private:
<i>
</i>var myFunction = function(){
}
Privileged:
<i>
</i>this.myFunction = function(){
}
Public:
<i>
</i>this.prototype.myFunction = function(){
}
[/QUOTE]
<i>
</i>function myFunction (){
}
[CODE]function Demo(strName){
var name = strName.toUpperCase();
}[/CODE]
[CODE]function Demo(strName){
var name = strName.toUpperCase();
this.name = name;
}[/CODE]
[CODE]
function Demo(strName){
var name = strName.toUpperCase();
this.name = name;
}
var person = new Demo("fred");
person.name = "bob";
alert (person.name); //shows "bob"
[/CODE]
[CODE]
function Demo(strName){
var name = strName.toUpperCase();
this.getName = function(){ return name; }
}
var person = new Demo("fred")
alert(person.name);// shows "undefined"
alert(person.getName());// shows "FRED"
[/CODE]
I think it's a misnomer to call any method in JavaScript a "privileged" method. Both public methods and private functions can access non-public information. If every method and function is equally privileged, then really there are no special privileges.[/QUOTE]
[code=php]function myParentFunc() //containing function object
{
var myDat; //private data
function myPrivateFunc()
{
//This function cannot call myPublic function
//This function can call myPublicFunction2
}
var myPublicFunction2 = function()
{
//This function can call myPrivateFunc
//This function cannot call myPublicFunction
}
this.myPublicFunction2 = myPublicFunction2;//create a reference
this.myPublicFunction = function()
{
//this function can call myPrivateFunc
//This function can call myPublicFunction2
}
}
var myObject = new myParentFunc();// new instance of the function object
myObject.myPublicFunction();
myObject.myPublicFunction2();[/code]
myParentFunc.prototype.myPublicButNonPrivilegedFunction = function() {
// This function cannot call anything that is private
alert(myDat); // Error: myDat is not defined
}
myObject.myPublicButNonPrivilegedFunction();
AluminX, try this in addition to your posted code.
[/QUOTE]
[CODE]
var myObject = (function(){
var myDat ="hello"; //private data
function myParentFunc() //containing function object
{
function myPrivateFunc()
{
//This function cannot call myPublic function
//This function can call myPublicFunction2
}
var myPublicFunction2 = function()
{
//This function can call myPrivateFunc
//This function cannot call myPublicFunction
}
this.myPublicFunction2 = myPublicFunction2;//create a reference
this.myPublicFunction = function()
{
//this function can call myPrivateFunc
//This function can call myPublicFunction2
}
}
myParentFunc.prototype.myPublicButNonPrivilegedFunction = function() {
// This function cannot call anything that is private
alert(myDat); // Error: myDat is not defined
}
return new myParentFunc();// new instance of the function object
}());
myObject.myPublicFunction();
myObject.myPublicFunction2();
myObject.myPublicButNonPrivilegedFunction();
[/CODE]
one solution?Well of course. I didn't say that was code I would write. I was posting an example for AluminX. All it was meant to do was illustrate privileged and non-privileged methods.
priveledge flows down functions, and peers can share parents using closure[/quote]
one solution?
priveledge flows down functions, and peers can share parents using closure
[/quote]
<i>
</i>function myTool () {
var updatePage = function(xmlDocument, index){
//Code here
self.getPrevListings(false);
self.getNextListings(false);
}
this.getNextListings = function(updateFlag){
//Code here
}
this.getPrevListings = function(updateFlag){
//Code here
}
}
[CODE]function myTool () {
[B]var self = this;[/B]
var updatePage = function(xmlDocument, index){
//Code here
self.getPrevListings(false);
self.getNextListings(false);
};
this.getNextListings = function(updateFlag){
//Code here
};
this.getPrevListings = function(updateFlag){
//Code here
};
}[/CODE]
priveledge flows down functions, and peers can share parents using closure
[/quote]
rnd_me: could you please clarify
priveledge flows down functions, and peers can share parents using closure
Thanks,
-C[/QUOTE]
[CODE]
function Demo(){
var a=1;
function DemoInner(){
var b=2;
function DemoInnerMost(){
var c=3;
alert( a+b+c); //shows 6
}
DemoInnerMost();
}
DemoInner();
}
Demo();
[/CODE]
0.1.9 — BETA 5.20