Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   помогите разобратся (https://javascript.ru/forum/misc/30907-pomogite-razobratsya.html)

bot87 20.08.2012 10:51

помогите разобратся
 
<script>
function Car( options ) {

  // some defaults
  this.doors = options.doors || 4;
  this.state = options.state || "brand new";
  this.color = options.color || "silver";

}

// A constructor for defining new trucks
function Truck( options){

  this.state = options.state || "used";
  this.wheelSize = options.wheelSize || "large";
  this.color = options.color || "blue";
}


// FactoryExample.js

// Define a skeleton vehicle factory
function VehicleFactory() {}

// Define the prototypes and utilities for this factory

// Our default vehicleClass is Car
VehicleFactory.prototype.vehicleClass = Car;

// Our Factory method for creating new Vehicle instances
VehicleFactory.prototype.createVehicle = function ( options ) {

  if( options.vehicleType === "car" ){
    this.vehicleClass = Car;
  }else{
    this.vehicleClass = Truck;
  }

  return new this.vehicleClass( options );

};

// Create an instance of our factory that makes cars
var carFactory = new VehicleFactory();
var car = carFactory.createVehicle( { 
            vehicleType: "car", 
            color: "yellow", 
            doors: 6 } );

// Test to confirm our car was created using the vehicleClass/prototype Car

// Outputs: true
alert( car instanceof Car ); 

// Outputs: Car object of color "yellow", doors: 6 in a "brand new" state
alert( car );
</script>

что происходит вот здесь?
VehicleFactory.prototype.vehicleClass = Car;

// Our Factory method for creating new Vehicle instances
VehicleFactory.prototype.createVehicle = function ( options ) {

  if( options.vehicleType === "car" ){
    this.vehicleClass = Car;
  }else{
    this.vehicleClass = Truck;
  }

  return new this.vehicleClass( options );

};


Почему оно не поддерживает наследование
function bl(){
	this.hoo=2
	}
	bl.prototype={rt:45}
function VehicleFactory() {}
VehicleFactory.prototype=new bl();

oneguy 20.08.2012 16:11

Я не совсем понял вопрос. Объясните, пожалуйста, какой результат вы хотите получить и какой на самом деле выходит.

nerv_ 20.08.2012 16:16

Цитата:

Сообщение от oneguy
Я не совсем понял вопрос.

я вообще ничего не понял :)
Цитата:

Сообщение от oneguy
какой результат вы хотите получить

взорвать наш мозг, не? :D


Часовой пояс GMT +3, время: 21:46.