【问题标题】:Please are there any effects for creating objects this way?请问这样创建对象有什么效果吗?
【发布时间】:2015-02-22 09:06:07
【问题描述】:
    <!doctype >
<html>
<meta utf="utf-8">
<header>
    <title>PRACTISE</title>
    <script >

function MakeMe(name , age , likes, shout){ //creates objects
    return {
        name : name,
        age : age,
        likes : likes,
        shout : function(){
            alert(shout + " " +last(this.likes));
        }
    }
}

function last(obj){ //gets last element of the array
    var el=obj.pop();
    obj.push(el);
    return el;
}

var dammy = MakeMe("Damilola" , 20, ["Jesus" , "Programming"], "Ewooooooo");//creates an object

dammy.shout();

var dara = MakeMe("Daramola" , 17, ["God" , "Gaming" , "Ed sheeran"] , "Leaveeeeeeeeeeee");//creates an object

// i wrote the following codes below to check if dammy and dara are really different objects
alert(last(dara.likes));
dara.shout();
dammy.likes.push("Taylor");
alert(last(dara.likes));
alert(last(dammy.likes));
dammy.shout();
    </script>
    </head>
</html>

我的主要问题是:与使用构造函数和使用“new”关键字的常规方法相反,这是一种在 javascript 中创建对象的好方法吗?如果使用上述方法创建对象有任何副作用。谢谢你

    //Normal way is
function MakeMe(name ,age, likes, shout){
   this.name = name;
   this.age= age;
   this.likes={"me", "you", "us"};
   this.shout = function(){
                alert(shout + " " +last(this.likes));
            };
}

var dammy = new MakeMe("D" , 17, ["God" , "Gaming" , "Lorde"] , "Loveee");

【问题讨论】:

  • 好像没问题,你可以用任何方式。

标签: javascript object constructor


【解决方案1】:

有一个区别,那个区别在于你返回的对象的原型。第二种方式,new 创建一个继承自 MakeMe 函数的 prototype 的对象,而第一个使用{} 只是创建一个与 MakeMe 无关的独立对象全部。

我已经在两个简化的例子中展示了这一点。

function Make1(value) {
  return {prop: value};
}

// Construction using {} returns a simple object that inherits nothing from Make1.
var object1 = Make1('foo');

// So I can add methods all I want...
Make1.prototype.showMe = function() {
  alert(this.prop);
}

// But object1 won't get them, and the line below shows that the method is not assigned.
alert(typeof object1.showMe);

// ..  and the line below will fail.
object1.showMe();

function Make2(value) {
  this.prop = value;
}

// Construct 'new' returns an object that inherits everything from the prototype of Make2.
var object2 = new Make2('bar');

// I can now add a method to the prototype.
Make2.prototype.showMe = function() {
  alert(this.prop);
}

// And call that method for the object and any other object that was constructed
// this way. They all share this common behaviour now.
object2.showMe();

现在这本身不是问题。这仅取决于您在特定情况下需要什么,您可以在项目中同时使用这两者。

我不记得我在哪里学到的,所以我查找了一些“随机”资源,这些资源似乎很好地解释了这个原型。

【讨论】:

  • 好答案.. 所以这两种方式都会创建对象。这是现在的目标,但是使用 new 关键字样式在构造函数和创建的对象之间创建了一种连接。就像java中的类一样。谢谢你。现在更清楚了
  • 与类非常相似,但略有不同。请参阅prototype based vs. class based inheritance 进行讨论。
【解决方案2】:

差异在原型级别很重要。 javascript 中的对象有一个原型链。通过这种方式,您可以将对象视为其每个原型的继承者。在第一种情况下,您没有原型链,因此不可能继承。

看看:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript

【讨论】:

  • 不是没有原型链,只是很短的一个:Object.prototypenull。 ;-)
  • 你说得对,这只是一个解释捷径!
猜你喜欢
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 2019-11-07
  • 2012-07-30
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 2010-10-07
相关资源
最近更新 更多