Search This Blog

Thursday, July 11, 2013

Javascript and objects

Many developers suppose that the document.getElementById() or the windows.print() functions are parts of javascript language. That opinion is wrong. The  Javascript is only one language which collaborate very well with four types of objects. These obects are  the Javascript Objects ,the Browser objects, the DOM Objects and the end The user defined objects.


JS Objects

JS Number
JS String
JS Date
JS Array
JS Boolean
JS Math
JS RegExp

JS Browser (Window) Objects

JS Window
JS Screen
JS Location
JS History
JS Navigator
JS PopupAlert
JS Timing
JS Cookies

DOM Objects

DOM Document
DOM Elements
DOM Attributes
DOM Events

Javascript user defined objects,inheritance and polymorfism



JavaScript is an Object Oriented Programming (OOP) language. A programming language can be called object-oriented if it provides four basic capabilities to developers:
  • Encapsulation . the capability to store related information, whether data or methods, together in an object
  • Aggregation . the capability to store one object inside of another object
  • Inheritance . the capability of a class to rely upon another class (or number of classes) for some of its properties and methods
  • Polymorphism . the capability to write one function or method that works in a variety of different ways


USER-DEFINED  Objects
I Approach.
in order to create user defined object we use the following frame of code:
       
         var newObject = new Object(); // Create the object
         newObject.firstProperty = "some string"; // Assign properties to the object
         newObject.secondProperty = "for example another string";

when we want to access this object we use the following frame of code:

document.write("firstProperty : " + newObject.firstProperty + "
");
document.write("secondProperty is : " + newObject.secondProperty + "
");

II Approach.(using user-defined function)

     function newObject (firstProperty , secondProperty){
this.firstProperty = firstProperty ;
this.secondProperty= secondProperty;
}
.....

var myNewObject = new newObject ("firstProperty value", "secondProp value");
document.write("firstProperty is : " + myNewObject.firstProperty + "
");
document.write("secondProperty is : " + myNewObject.secondProperty+ "
");

Assigning methods  to object.
Objects are composed of attributes. If an attribute contains a function, it is considered to be a method of the object otherwise, the attribute is considered a property.

function someFunctionOfThirdProperty(parammeter){ this.thirdProperty= parammeter; } function newObject (firstProperty , secondProperty){ this.firstProperty = firstProperty ; this.secondProperty= secondProperty; this.someFunctionOfThirdProperty=someFunctionOfThirdProperty;//!!!! } .... var myNewObject = new newObject ("firstProperty value", "secondProp value"); myNewObject.someFunctionOfThirdProperty("parammeterValue"); document.write("firstProperty is : " + myNewObject.firstProperty + " "); document.write("secondProperty is : " + myNewObject.secondProperty+ " "); document.write("thirdProperty is : " + myNewObject.thirdProperty+ " ");
with keyword
 with (object){
    properties used without the object name and dot
}
replace the function above with this function:
function someFunctionOfThirdProperty(parammeter){
     with(this){
       thirdProperty= parammeter; 
     }
}

Wednesday, July 10, 2013

Scripting language

A high-level programming language that is interpreted by another program at runtime(interpreter) rather than compiled in the design time by Compilers  as other programming languages (such as C and C#) are. 
The spectrum of scripting languages ranges from very small and highly domain-specific languages to general-purpose programming languages. The term script is typically reserved for small programs (up to a few thousand lines of code)

Types of scripting languages

  • Job control languages and shell (shell script)
  • GUI scripting (macros)
  • Application-specific language(Lisp)
  • Text processing languages(grep)
  • General-purpose dynamic languages(Perl)
  • Extension/embeddable languages(ECMAScript or javascript)

The key design principles within JavaScript are taken from the Self and Scheme programming languages. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles.It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter the document content that was displayed..

Definitions
1)A multi-paradigm programming language is a programming language that supports more than one programming paradigm. As Leda designer Timothy Budd puts it: "The idea of a multiparadigm language is to provide a framework in which programmers can work in a variety of styles, freely intermixing constructs from different paradigms." The design goal of such languages is to allow programmers to use the best tool for a job, admitting that no one paradigm solves all problems in the easiest or most efficient way.

2)Icomputer scienceimperative programming is a programming paradigm that describes computation in terms of statements that change a program state. In much the same way that imperative moodin natural languages expresses commands to take action, imperative programs define sequences of commands for the computer to perform.

3)In computer sciencefunctional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state(see also lambda calculus)

Friday, July 5, 2013

Using assembler in C#

The only one easy way to do that is the following:

1) to create some dll  in C++ which contains assembler code.

// Somename.cpp

#include "SysInfo.h"

BOOL __stdcall DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved) {
 return  TRUE;
}
//write some functions in C
extern "C" __declspec(dllexport) int __stdcall someFunction() {
 int retVal;
         //write some C code
 // write some code in assembler
_asm { mov eax, 1 mov retVal, eax } // return result return (retVal)
//write some other functions in C
2)to use dumpbin tool in order to get thwe right name of the Function. Something like that:                             dumpbin -exports Somename.dll    in order to get some output like the following:         ordinal hint RVA name
          1    0 00001005 ?someFunction@@YGXPAD@Z
The Compiler in order to prepare functionw for overloading gives to functions new names,
This step can be ommited if you use the extern "C" keyward . In this manner the C compiler is informed to don't decorate the function name.
Here @ shows the function uses the standard calling convention stdcall

stdcall

The stdcall calling convention is a variation on the Pascal calling convention in which the callee is responsible for cleaning up the stack, but the parameters are pushed onto the stack in right-to-left order, as in the _cdecl calling convention. Registers EAX, ECX, and EDX are designated for use within the function. Return values are stored in the EAX register.

stdcall is the standard calling convention for the Microsoft Win32 API and for Open Watcom C++.
more about calling conventions here 3)to call functions in dll  through some C# program
/ Main.cs


using System;
using System.Runtime.InteropServices;

class MainClass {
 [DllImport("Somename.dll")]
 static extern int someFunction();
 
 
 // main program
 public static void Main() {
  
  //get  someFunction() result

  try {
   int param = someFunction();
   Console.WriteLine("Param value = {0}", param .ToString()                    );
  }
  catch (DllNotFoundException e) {
   Console.WriteLine(e.ToString());
  }
  catch (EntryPointNotFoundException e) {
   Console.WriteLine(e.ToString());
  }
  
  
 }
};

Wednesday, July 3, 2013

Callback Functions

A callback function is code within a managed application that helps an unmanaged DLL function complete a task. Calls to a callback function pass indirectly from a managed application, through a DLL function, and back to the managed implementation. Some of the many DLL functions called with platform invoke require a callback function in managed code to run properly.
Generally speaking a callback is executable code that is passed as an argument to other code

See in the following function's signature:
              BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam)  
lp prefix comes from the long pointer term.Thus the lpEnumFunc is a pointer to a callback function.

The EnumWindows function enumerates through all existing windows on your computer, calling the callback function to perform a task on each window. Thiw function belongs to the Win32 API (user32.dll).
Also,in the following example:
EnumWindows  unmanaged DLL function. 
CallBack(int hwnd, int lparam) is the managed  callback function
hwnd:handle to the window
lparam:application defined parameter

using System;
using System.Runtime.InteropServices;

public delegate bool CallBack(int hwnd, int lParam);

public class EnumReportApp
{
    [DllImport("user32")]
    public static extern int EnumWindows(CallBack x, int y); 

    public static void Main() 
    {
        CallBack myCallBack = new CallBack(EnumReportApp.Report);
        EnumWindows(myCallBack, 0);
    }

    public static bool Report(int hwnd, int lParam)
    { 
        Console.Write("Window handle is ");
        Console.WriteLine(hwnd);
        //Callback functions generally return nonzero values to indicate success         //and zero to indicate failure. This example explicitly sets the return v        //alue to true to continue the enumeration.

        return true;
    }
}

Similar enumeration functions are: EnumFontFamilies, EnumPrinters

The usage of a callback is usually in asynchronous logic.


About Me

An seasoned developer, architect and with some Team Leading exposure, offering full project life cycle experiences within multi cultural, multi National environments and within differing business streams and all primarily on a .Net platform.