Configure HTML/JavaScript

Saturday, September 4, 2010

example of object in javascript (privileges)

javascript private members of a object

Private Members in JavaScript

Douglas Crockford
www.crockford.com



JavaScript is the world's most misunderstood programming language. Some believe that it lacks the property of information hiding because objects cannot have private instance variables and methods. But this is a misunderstanding. JavaScript objects can have private members. Here's how.
Objects

JavaScript is fundamentally about objects. Arrays are objects. Functions are objects. Objects are objects. So what are objects? Objects are collections of name-value pairs. The names are strings, and the values are strings, numbers, booleans, and objects (including arrays and functions). Objects are usually implemented as hashtables so values can be retrieved quickly.

If a value is a function, we can consider it a method. When a method of an object is invoked, the this variable is set to the object. The method can then access the instance variables through the this variable.

Objects can be produced by constructors, which are functions which initialize objects. Constructors provide the features that classes provide in other languages, including static variables and methods.
Public

The members of an object are all public members. Any function can access, modify, or delete those members, or add new members. There are two main ways of putting members in a new object:
In the constructor

This technique is usually used to initialize public instance variables. The constructor's this variable is used to add members to the object.

function Container(param) {
this.member = param;
}

So, if we construct a new object

var myContainer = new Container('abc');

then myContainer.member contains 'abc'.
In the prototype

This technique is usually used to add public methods. When a member is sought and it isn't found in the object itself, then it is taken from the object's constructor's prototype member. The prototype mechanism is used for inheritance. It also conserves memory. To add a method to all objects made by a constructor, add a function to the constructor's prototype:

Container.prototype.stamp = function (string) {
return this.member + string;
}

So, we can invoke the method

myContainer.stamp('def')

which produces 'abcdef'.
Private

Private members are made by the constructor. Ordinary vars and parameters of the constructor becomes the private members.

function Container(param) {
this.member = param;
var secret = 3;
var that = this;
}

This constructor makes three private instance variables: param, secret, and that. They are attached to the object, but they are not accessible to the outside, nor are they accessible to the object's own public methods. They are accessible to private methods. Private methods are inner functions of the constructor.

function Container(param) {

function dec() {
if (secret > 0) {
secret -= 1;
return true;
} else {
return false;
}
}

this.member = param;
var secret = 3;
var that = this;
}

The private method dec examines the secret instance variable. If it is greater than zero, it decrements secret and returns true. Otherwise it returns false. It can be used to make this object limited to three uses.

By convention, we make a private that parameter. This is used to make the object available to the private methods. This is a workaround for an error in the ECMAScript Language Specification which causes this to be set incorrectly for inner functions.

Private methods cannot be called by public methods. To make private methods useful, we need to introduce a privileged method.
Privileged

A privileged method is able to access the private variables and methods, and is itself accessible to the public methods and the outside. It is possible to delete or replace a privileged method, but it is not possible to alter it, or to force it to give up its secrets.

Privileged methods are assigned with this within the constructor.

function Container

in reference to: Contribute with Sidewiki - Toolbar Help (view on Google Sidewiki)

Thursday, June 24, 2010

learning spanish

few words

I -> estoy
Excuse me -> perdone senora

good morning -> buenos dias
if not morning -> buenos noches

how are you feeling -> como esta?
i am fine -> Estoy ben.

what is your name -> como iiama

my name is anirudda -> me iiama aniruddha

its nice to meet you -> mucho gusto

Thanks

in reference to: Edit My Profile (view on Google Sidewiki)

Wednesday, May 26, 2010

JSON (javascript object notification)

Hi all,

This is one of the technology you can use to make your javascript and ajax objects more powerfull.

declaring object:

var newJsonObj = {}
vat newJsonArr = [];

newJsonObj = { "tty" : android,
"name", aniruddha das,
"posts" : [
{"":thid,}
]
}

in reference to: 4shared.com - free file sharing and storage (view on Google Sidewiki)

Sunday, May 23, 2010

generating a popup calender in javascript

// written by Tan Ling Wee on 2 Dec 2001
// last updated 10 Apr 2002
// email : fuushikaden@yahoo.com

var fixedX = -1 // x position (-1 if to appear below control)
var fixedY = -1 // y position (-1 if to appear below control)
var startAt = 1 // 0 - sunday ; 1 - monday
var showWeekNumber = 1 // 0 - don't show; 1 - show
var showToday = 1 // 0 - don't show; 1 - show
var imgDir = "images/" // directory for images ... e.g. var imgDir="/img/"

var gotoString = "Go To Current Month"
var todayString = "Today is"
var weekString = "Wk"
var scrollLeftMessage = "Click to scroll to previous month."
var scrollRightMessage = "Click to scroll to next month."
var selectMonthMessage = "Click to select a month."
var selectYearMessage = "Click to select a year."
var selectDateMessage = "Select [date] as date." // do not replace [date], it will be replaced by date.

var crossobj, crossMonthObj, crossYearObj, monthSelected, yearSelected, dateSelected, omonthSelected, oyearSelected, odateSelected, monthConstructed, yearConstructed, intervalID1, intervalID2, timeoutID1, timeoutID2, ctlToPlaceValue, ctlNow, dateFormat, nStartingYear

var bPageLoaded=false
var ie=document.all
var dom=document.getElementById

var ns4=document.layers
var today = new Date()
var dateNow = today.getDate()
var monthNow = today.getMonth()
var yearNow = today.getYear()
var imgsrc = new Array("drop1.gif","drop2.gif","left1.gif","left2.gif","right1.gif","right2.gif")
var img = new Array()

var bShow = false;

/* hides and objects (for IE only) */
function hideElement( elmID, overDiv )
{
if( ie )
{
for( i = 0; i < document.all.tags( elmID ).length; i++ )
{
obj = document.all.tags( elmID )[i];
if( !obj || !obj.offsetParent )
{
continue;
}

// Find the element's offsetTop and offsetLeft relative to the BODY tag.
objLeft = obj.offsetLeft;
objTop = obj.offsetTop;
objParent = obj.offsetParent;

while( objParent.tagName.toUpperCase() != "BODY" )
{
objLeft += objParent.offsetLeft;
objTop += objParent.offsetTop;
objParent = objParent.offsetParent;
}

objHeight = obj.offsetHeight;
objWidth = obj.offsetWidth;

if(( overDiv.offsetLeft + overDiv.offsetWidth ) <= objLeft );
else if(( overDiv.offsetTop + overDiv.offsetHeight ) <= objTop );
else if( overDiv.offsetTop >= ( objTop + objHeight ));
else if( overDiv.offsetLeft >= ( objLeft + objWidth ));
else
{
obj.style.visibility = "hidden";
}
}
}
}

/*
* unhides and objects (for IE only)
*/
function showElement( elmID )
{
if( ie )
{
for( i = 0; i < document.all.tags( elmID ).length; i++ )
{
obj = document.all.tags( elmID )[i];

if( !obj || !obj.offsetParent )
{
continue;
}

obj.style.visibility = "";
}
}
}

function HolidayRec (d, m, y, desc)
{
this.d = d
this.m = m
this.y = y
this.desc = desc
}

var HolidaysCounter = 0
var Holidays = new Array()

function addHoliday (d, m, y, desc)
{
Holidays[HolidaysCounter++] = new HolidayRec ( d, m, y, desc )
}

if (dom)
{
for (i=0;i<

in reference to: Google (view on Google Sidewiki)

opening a new divisin window in javascript and handling ajax operation

//generate the message window

function showWindow(varTotalTax,evt)
{

//var evt = (evt) ? evt : ((window.event) ? window.event : "")
if(evt)
{
var elem = (evt.target) ? evt.target : evt.srcElement
var id = elem.id;
var x = evt.clientX - 10;
var y = evt.clientY + document.body.scrollTop+4;
var divObj = document.getElementById("popupdiv");
divObj.style.display = "block";
divObj.style.position = "absolute";
divObj.style.left = x;
divObj.style.top = y;
if(varTotalTax !=0)
{
divObj.innerHTML = "

"+varTotalTax+"

"
}
else
{
divObj.innerHTML = "Notes not available
X "
}
}
}

// close the generated window
function divclose(name)
{
var divObj;
divObj= document.getElementById(name);
divObj.style.display = "none";
}

in reference to: Google (view on Google Sidewiki)

sending ajax request

function selectState(e)
{
if(e==212)
{
//document.getElementById("countryLvl").innerHTML = "";
document.getElementById("stateLebel").innerHTML = "County : ";
document.getElementById("state").innerHTML = "";
//document.getElementById("countryLvl").innerHTML = "";

}
else
{
document.getElementById("stateLebel").innerHTML = "State : ";
if(xmlHttp)
{
try {
xmlHttp.open("GET","SelectState.php?country="+e, true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
} catch(e) {
alert("Can't connect to server:\n" + e.toString());
}
}
}
}

/*function selectCity(e)
{
var myDivLebel = document.getElementById("cityLebel"); //.innerHTML;
myDivLebel.innerHTML = "City : ";

if(xmlHttp)
{
try {
var country = document.getElementById("countryId").value;
xmlHttp.open("GET","SelectState.php?country="+country+"&state="+e, true);
xmlHttp.onreadystatechange = handleServerResponseCity;
xmlHttp.send(null);
} catch(e) {
alert("Can't connect to server:\n" + e.toString());
}
}
}*/

function handleServerResponse()
{
myDiv = document.getElementById("state");
// display the status o the request
/* if (xmlHttp.readyState == 1)
{
myDiv.innerHTML = "Request status: 1 (loading)
";
}
else if (xmlHttp.readyState == 2)
{
myDiv.innerHTML = "Request status: 2 (loading)
";
}
else if (xmlHttp.readyState == 3)
{
myDiv.innerHTML = "Request status: 3 (loading)
";
}
else*/ if (xmlHttp.readyState == 4)
{
// continue only if HTTP status is "OK"
if (xmlHttp.status == 200)
{
try {
response = xmlHttp.responseText;
//myDiv.innerHTML += "Request status: 4 (complete). Server said:
";
myDiv.innerHTML = response;
} catch(e) {
alert("Error reading the response: " + e.toString());
}
}
}
}

function handleServerResponseCity()
{
myDiv = document.getElementById("city");
/* if (xmlHttp.readyState == 1)
{
myDiv.innerHTML = "Request status: 1 (loading)
";
}
else if (xmlHttp.readyState == 2)
{
myDiv.innerHTML = "Request status: 2 (loading)
";
}
else if (xmlHttp.readyState == 3)
{
myDiv.innerHTML = "Request status: 3 (loading)
";
}
else*/ if (xmlHttp.readyState == 4)
{
// continue only if HTTP status is "OK"
if (xmlHttp.status == 200)
{
try {
response = xmlHttp.responseText;
//myDiv.innerHTML += "Request status: 4 (complete). Server said:
";
myDiv.innerHTML = response;
} catch(e) {
alert("Error reading the response: " + e.toString());
}
}
}
}

in reference to: Google (view on Google Sidewiki)