JavaScript Object-Oriented Concept Essential Part1

1. Everything excpet primitive types in JavaScript is object.
2. Primitive types include Undefined, Null, Boolean, Number and String.
3. JavaScript has three kinds of objects.
  • 3.1 Build-in objects: Array, Image, Date...
  • 3.2 Host objects: window, document, forms..
  • 3.3 User-defined.
4. Primitive types has associated object.
  • Ex, var stringObj = new String("create String object");
5. new is used to create objects.
6. this is used to refer the current object.
7. class in Javascript is defiend as function.
  • class field and function is defined by this. Ex,
  • function Cicle(radius){
  • this. radius = radius; // defined class field, assigned by parameter of constructor
  • this. getArea = getArea; // defined class function, getArea is a function
  • function getArea(){return this. radius;}// still use this to refer current object
  • //the function getArea can also be defiend outside the class, with this. radius always
  • //refer the object who use the funtion.
  • }
8. Many ways to define a function, and whenever you create a function, you create a object of the function
  • 8.1 functionName = function("para1","para2");// the last para is the body of the //function, you can use the functionName as the callable block, and it is an object
  • 8.2 var myFunctionObj = new Function();
  • 8.3 var myFunctionObj = Function();
  • 8.4 var myFunctionObj = function(){};
  • 8.5 function myFunctionObj(){};
9. A variable can not have field. Only object can have.
Ex, var primitiveString = "I am string varible" ;
primitiveString.prop = "my property"; // this is wrong

While var stringObj = new String("make a string object");
stringObj.prop = "my property"; //this is correct

评论

此博客中的热门博文

Nu förbjuder Kina handel med elfenben

Fader av pingyins

Kineserna vill köpa Volvos kompetens