1.  
  2. $(function () {
  3. Array.prototype.contains = function (obj) {
  4. a = this;
  5. for (var i = 0; i < a.length; i++) {
  6. if (a[i] === obj) {
  7. return i;
  8. }
  9. }
  10. return -1;
  11. };
  12.  
  13. var console = {};
  14. console.log = function (msg) {
  15. $("#result").append(msg.toString() + String.fromCharCode(60) + "br/" + String.fromCharCode(62));
  16. };
  17.  
  18. function Car(brand, model) {
  19. // private member
  20. var _mileage = 0;
  21.  
  22. // properties
  23. this.brand = brand;
  24. this.model = model;
  25.  
  26. // methods
  27. this.run = function (mileage) {
  28. _mileage = mileage;
  29. };
  30. this.getMileage = function () {
  31. return _mileage;
  32. };
  33. }
  34.  
  35. // class level methods
  36. Car.prototype.getCarData = function () {
  37. return this.brand + " " + this.model;
  38. };
  39.  
  40. var myCar = new Car("VW", "POLO");
  41. myCar.run(100);
  42.  
  43. var carData = myCar.getCarData();
  44. console.log(carData);
  45.  
  46. var mileage = myCar.getMileage();
  47. console.log(mileage);
  48.  
  49. var cars = [];
  50. cars.push(new Car("VW", "POLO"));
  51. cars.push(new Car("VW", "POLO"));
  52. cars.push(myCar);
  53.  
  54. console.log(cars.indexOf(myCar));
  55. console.log(cars.contains(myCar));
  56. console.log($.inArray(myCar, cars));
  57.  
  58. cars.forEach(function (item, index) {
  59. console.log(item.brand + " " + item.model);
  60. });
  61.  
  62. for (var i = 0; i < cars.length; i++) {
  63. console.log(cars[0].brand + " " + cars[0].model);
  64. }
  65.  
  66. $.each(cars, function (index, item) {
  67. console.log(item.brand + " " + item.model);
  68. });
  69.  
  70. for (var prop in myCar) {
  71. if (typeof myCar[prop] == "string") {
  72. console.log(prop + "=" + myCar[prop]);
  73. }
  74. }
  75. });
VW POLO
100
2
2
2
VW POLO
VW POLO
VW POLO
VW POLO
VW POLO
VW POLO
VW POLO
VW POLO
VW POLO
brand=VW
model=POLO