Wednesday, November 9, 2016

OO in JavaScript

A method is actually a function definition stored as a property value.
If you access the fullName method, without (), it will return the function definition:

 http://www.w3schools.com/js/js_objects.asp
 
在面向对象编程中,类(class)是对象(object)的模板,定义了同一组对象(又称"实例")共有的属性和方法。Javascript语言不支持"类",但是可以用一些变通的方法,模拟出"类"。 Javascipt语法不支持"类"(class).

 http://www.ruanyifeng.com/blog/2012/07/three_ways_to_define_a_javascript_class.html
  
JavaScript objects cannot be compared.

var x = "John";           
var y = new String("John");
// (x === y) is false because x and y have different types (string and object) 
 
var x = new String("John");           
var y = new String("John");

// (x == y) is false because x and y are different objects// (x == x) is true because both are the same object

http://www.w3schools.com/js/js_strings.asp


Primitive values, like "John Doe", cannot have properties or methods (because they are not objects).
But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.
The length property returns the length of a string:
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
 
 http://www.w3schools.com/js/js_string_methods.asp

No comments:

Post a Comment