2016-09-09 2 views
1

Wie Wert des Objekts ändern, wenn ein Argument festgelegt ist und nicht, wenn manWert der Funktion ändern oder mit einem Prototyp gleich bleiben?

Beispiel ist:

//parameter is a object 
var Main = function(parameter) 
{ 
    this.num = 1; 
    this.string = "this" 
} 

Main.prototype.constructor = Main; 

//source is an object 
Main.prototype.copy = function(source) 
{ 
    Main.prototype.copy.call(this, source); 

    this.num = source.num; 
    this.string = source.string; 


    return this; 
} 

//set sub2 to have num is 4 instead of 1 
var sub2 = new Main({num:4}); 

und ich möchte wissen, wie man einen Prototyp einrichten, die einer Änderung des ursprünglichen zulassen Objekt, wenn ein Argument et ist, aber den Standard von der Main-Funktion verwenden, wenn ein Argument nicht gesetzt ist, oder würde ich eine Funktion verwenden, um die Werte auf den Standardwert zu setzen, wenn ein Argument leer ist und dann auf das Parameterobjekt gesetzt wird?

Dank

+0

Bitte zeigen Sie den tatsächlichen Code. 'var object = new object ...' führt immer zu einem Typfehler. – Bergi

+0

das war nur ein Beispiel, der tatsächliche Code stammt aus drei js, wo Sie Parameter deklarieren können, die Sie ändern möchten, zum Beispiel wenn Sie ein Material erstellen, das Sie var mat1 = new THREE.MeshPhongMaterial ({color: 0xffffff}); –

+0

Sie fragen also, was der Unterschied zwischen der Eigenschaft 'colour' des Konstruktorarguments und der Eigenschaft' mat1.color' ist? – Bergi

Antwort

2

Der einzige Unterschied ist in der Nomenklatur:

// So if you declare object as 
 
var obj1 = {param1: "value1"}; 
 

 
// there is absolutely no difference if you declare it as 
 
var obj2 = new Object({param2: "value2"}); 
 

 
// Its because every object inherits from Object, 
 
// no matter which way is defined. 
 

 
// Test 
 
console.log(typeof obj1); 
 
console.log(typeof obj2); 
 

 
console.log(obj1 instanceof Object); 
 
console.log(obj2 instanceof Object); 
 

 
// Reaction to your comment below: 
 
// Its as complicated as confusing. In this case 'var obj = {}' you are 
 
// creating object and curly brackets delimit object definition. 
 
// But in this case 'var obj2 = function() {}' you are creating function 
 
// and curly brackets represents 'code block', not object literal. 
 
// But ... function is object also... 
 

 
var myFunction = function() {}; 
 
console.log(myFunction instanceof Object); 
 

 
// And while this 'var obj = function(){para: 1}' is valid but meaningless 
 
// code, this 'var obj = function(){this.para: 1}' is as valid as 
 
// well as frequently used expression to create ... object. But object in 
 
// another sense... Its really confusing.

Aber ersten Ausdrucks, sein genannten "Literalausdruck Objekt", ist bevorzugt und sicheren Weg, um Objekte zu erstellen. Weil bösartiger Code das Objektverhalten überschreiben kann. Wenn Sie also ein Objekt als neues Objekt() erstellen, können Sie etwas Unerwartetes erzeugen. Aber der objektwörtliche Ausdruck kann nicht übergangen werden.

+0

Ah so würde dies nicht funktionieren, wenn Sie ein Objekt als var obj = function() {para: 1}? –

+0

@DariusBigbee: Das ist ein Syntaxfehler. – Bergi

+0

Falsch. Sein syntaktisch gültiger Code. – WaldemarIce

0

Ich dachte, es dauert eine Weile, ich entschied mich, durch die drei Js-Code zu sehen, wie Eigenschaften festgelegt wurden und ich herausgefunden, dass ich Eigenschaften durch das Objekt gehen muss, um die Schlüssel und die Schlüssel zu erhalten des Objekts: Hier ist ein Beispiel zum Erstellen von Planeten

/* parameters = { 
    radius : <float> 
    tilt : <float> 
    distance : <float> 
    days : <float> 
    years : <float> 
    diffuse : <texture> 
    specular : <texture> 
    normal : <texture> 
    bump : <texture> 
} 
*/ 
var CelestialObject = function (parameters) { 
    var toRadians = 180/Math.PI; 

    this.radius = 1; 
    this.tilt = 23.5; 
    this.distance = 0; 
    this.days = 1; 
    this.years = 1; 
    this.orbitEccentricity = 0; 
    this.diffuse = null; 
    this.specular = null; 
    this.normal = null; 
    this.bump = null; 


    var material = new THREE.MeshPhongMaterial();//mesh material 
    var geometry = new THREE.SphereGeometry(this.radius, 64, 64);//sphere geometry 
    var mesh = new THREE.Mesh(geometry, material);//mesh 

    //Apply Textures 
    if (this.diffuse = null) 
     material.map = this.diffuse; 
    else 
     material.color = new THREE.Color(0x777777); 
    if (this.specular = null) 
     material.map = this.specular; 
    if (this.normal = null) 
     material.normalMap = this.diffuse; 
    if (this.bump = null) 
     material.bumpMap = this.bump; 

    //Set Tilt 
    this.SetTilt = function() { 
     mesh.rotateZ(this.tilt * toRadians); 
    } 

    //Rotate 
    var Rotate = function (mesh) { 

     requestAnimationFrame(Rotate); 

     var hoursInDay = 24; 
     var angle = (360/(this.days * hoursInDay)) * toRadians; 

     mesh.rotateY(angle); 
    } 

    //Revolve 
    var Revolve = function (mesh) { 

     requestAnimationFrame(Revolve); 

     var time = new Date().getTime(); 
     var hoursInYears = this.years * 365 * 24; 

     time += 360/hoursInYears; 

     var xPos = Math.cos(time) * this.distance; 
     var zPos = Math.sin(time) * this.distance * this.orbitEccentricity; 


     mesh.position.set(xPos, 0, zPos); 
    } 

    //set mesh 
    this.setMesh = function (customMesh, meshURL) { 
     var jsonLoader = new THREE.JSONLoader(); 
     if (!customMesh) 
      mesh = new THREE.Mesh(new THREE.SphereGeometry(this.radius, 64, 64), new THREE.MeshPhongMaterial({ color: 0x777777 }));//sphere 
     else 
      mesh = jsonLoader.load(meshURL); 
    } 

    //set values 
    for (var key in parameters) { 
      var newValue = parameters[key]; 
      this[key] = newValue; 
    } 


    Rotate(mesh); 
    Revolve(mesh); 

    return this; 
}; 
//create new planets here with different values 
var earth = new CelestialObject();//use default parameters 
var mars = new CelestialObject({radius:.4,tilt:25,distance:141,days:1.5,years:2});//uses new parameters 
+0

Ich verstehe nicht, was Sie meinen, was ist der Zweck dieses Objekts Main. Können Sie mir ein Beispiel für die Verwendung dieses Objekts schreiben? – WaldemarIce

+0

Beispiel wurde für das, was ich benutze es für aktualisiert –

Verwandte Themen