如果设置了参数,如何更改对象的值,如果是
则不更改示例:
//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});
我想知道如何设置一个原型,如果参数是et,可以允许更改原始对象但是如果参数未设置则使用Main函数的默认值,或者我将使用函数来设置如果参数为空,则为默认值,如果为参数,则设置为参数对象?
由于
答案 0 :(得分:2)
唯一的区别在于命名法:
// 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.
但是第一个表达式,它被称为“对象文字表达式”,是创建对象的首选和安全的方法。因为某些恶意代码可以覆盖Object行为。因此,如果您将对象创建为新的Object(),则可以创建意外的内容。但是对象字面表达不能被覆盖。
答案 1 :(得分:0)
我想出来花了一段时间,我决定查看三个js代码,看看属性是如何设置的,我想出要设置属性我需要通过对象获取密钥并设置密钥对象:这是一个创建行星的例子
/* 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