Wednesday, September 14, 2011

Javascript protoType

http://www.codeproject.com/KB/scripting/Observer_Pattern_JS.aspx

function ArrayList() {
            //initialize with an empty array
            this.aList = [];
        }

        ArrayList.prototype =
        {
            Count: function () {
                return this.aList.length;
            },
            Add: function (object) {
                //Object are placed at the end of the array
                return this.aList.push(object);
            },
            GetAt: function (index) //Index must be a number
            {
                if (index > -1 && index < this.aList.length)
                    return this.aList[index];
                else
                    return undefined; //Out of bound array, return undefined
            },
            Clear: function () {
                this.aList = [];
            },
            RemoveAt: function (index) // index must be a number
            {
                var m_count = this.aList.length;

                if (m_count > 0 && index > -1 && index < this.aList.length) {
                    switch (index) {
                        case 0:
                            this.aList.shift();
                            break;
                        case m_count - 1:
                            this.aList.pop();
                            break;
                        default:
                            var head = this.aList.slice(0, index);
                            var tail = this.aList.slice(index + 1);
                            this.aList = head.concat(tail);
                            break;
                    }
                }
            },
            Insert: function (object, index) {
                var m_count = this.aList.length;
                var m_returnValue = -1;

                if (index > -1 && index <= m_count) {
                    switch (index) {
                        case 0:
                            this.aList.unshift(object);
                            m_returnValue = 0;
                            break;
                        case m_count:
                            this.aList.push(object);
                            m_returnValue = m_count;
                            break;
                        default:
                            var head = this.aList.slice(0, index - 1);
                            var tail = this.aList.slice(index);
                            this.aList = this.aList.concat(tail.unshift(object));
                            m_returnValue = index;
                            break;
                    }
                }
                return m_returnValue;
            },
            IndexOf: function (object, startIndex) {
                var m_count = this.aList.length;
                var m_returnValue = -1;

                if (startIndex > -1 && startIndex < m_count) {
                    var i = startIndex;

                    while (i < m_count) {
                        if (this.aList[i] == object) {
                            m_returnValue = i;
                            break;
                        }

                        i++;
                    }
                }
                return m_returnValue;
            },
            LastIndexOf: function (object, startIndex) {
                var m_count = this.aList.length;
                var m_returnValue = -1;

                if (startIndex > -1 && startIndex < m_count) {
                    var i = m_count - 1;

                    while (i >= startIndex) {
                        if (this.aList[i] == object) {
                            m_returnValue = i;
                            break;
                        }

                        i--;
                    }
                }
                return m_returnValue;
            },
            ConvertToJSON: function () {
                return JSON.stringify(this.aList);
            },
            AscSort: function () {
                this.aList.sort(function (firstObject, secondObject) {
                    if (firstObject.PropertyName < secondObject.PropertyName)
                        return -1;
                    if (firstObject.PropertyName > secondObject.PropertyName)
                        return 1;
                    return 0;
                })
            }
        };


function PropertyLevel() {
    this.PropertyId = "";
    this.PropertyName = "";
    this.PropertyValue = [];
}



        var myarrayobject = new ArrayList();
        function CreateRoleProperty(propertyName, propertyId, propertyValueKey, propertyValueText) {
            var objPropertyLevel = new PropertyLevel();
            objPropertyLevel.PropertyId = propertyId;
            objPropertyLevel.PropertyName = propertyName;
            var PropertyValue = new ListEntity();
            PropertyValue.Key = propertyValueKey;
            PropertyValue.Value = propertyValueText;
            objPropertyLevel.PropertyValue[0] = PropertyValue;
            // Add values at the end of the array
            myarrayobject.Add(objPropertyLevel);

           // Ascending Order
          myarrayobject.AscSort();
        }

No comments:

Post a Comment