[英]How do you create a method for a custom object in JavaScript?
Is it like... 是不是......
var obj = new Object();
obj.function1 = function(){
//code
}
or something like that? 或类似的东西?
You can see from the answers that you have already that there is more than one way. 您可以从答案中看到,您已经有多种方式。
#1
var o = new Object();
o.method = function(){}
#2
var o = new Object();
o.prototype.method = function(){}
#3
function myObject() {
this.method = function(){}
}
var o = new myObject();
#4
function myObject() {}
myObject.prototype.method = function(){}
var o = new myObject();
#5
var o = {
method: function(){}
}
#3 and #4 are using a constructor function. #3和#4使用构造函数。 this means you can use them to create a number of objects of the same 'class' (classes don't really exist in JavaScript)
这意味着您可以使用它们来创建相同“类”的多个对象(JavaScript中并不存在类)
#4 is different to #3 because all objects constructed with #4 will share an identical 'method' method because it is a property of their prototype. #4与#3不同,因为用#4构造的所有对象将共享相同的'方法'方法,因为它是其原型的属性。 This saves memory (but only a very tiny amount) and if you change the method of the prototype, all #4 objects will immediately be updated - even if they've already been instantiated.
这样可以节省内存(但只是非常小的数量),如果更改原型的方法,所有#4对象将立即更新 - 即使它们已经被实例化。
#1, #2 and #5 are all pretty much equivalent. #1,#2和#5都非常相同。 This is because there will probably only ever be one of them at a time, so the fact that #2 has the method added to the prototype doesn't really matter.
这是因为一次可能只有其中一个,所以#2将方法添加到原型中的事实并不重要。 (not taking cloning into account)
(不考虑克隆)
There are still more ways of adding methods to objects using factories with closure or adding 'static' properties/methods to functions or private nested functions... :) 还有更多方法可以使用具有闭包的工厂或向函数或私有嵌套函数添加“静态”属性/方法来向对象添加方法... :)
var newObj = {
met1 : function () {
alert('hello');
}
};
Then, the method can be called like such : 然后,可以像这样调用该方法:
newObj.met1();
Btw, when declaring a new object, use the object literal ( {}
), not the new Object()
constructor. 顺便说一句,在声明一个新对象时,使用对象文字(
{}
),而不是new Object()
构造函数。
Generally use the prototype property: 一般使用prototype属性:
function YourObject()
{
//
}
YourObject.prototype.yourMethod= function()
{
//
}
One thing I haven't seen anyone mention yet is why you might want to use the prototype
property over, say, object-literal notation: doing so ensures the function definition gets shared across all instances of the objects created from your function prototype, rather than once per instantiation. 我还没有看到有人提到的一件事是为什么你可能想要使用
prototype
属性,比如对象 - 文字符号:这样做可以确保函数定义在从函数原型创建的对象的所有实例中共享,而不是每次实例化一次。
Don't worry bro, here the code is: 别担心兄弟,这里的代码是:
var myObj=function(){
var value=null
this.setValue=function(strValue){
this.value=strValue;
};
this.getValue=function(){
return this.value;
};
};
You can call this object like this: 您可以像这样调用此对象:
var obj= new myObj();
obj.setValue("Hi!");
alert(obj.getValue());
Function.prototype.implement = function(member, value) {
this[member] = value;
return this;
}
function MyFunction() {
//...
}
(function($){
$.implement("a", "blabla")
.implement("b", function(){ /* some function */ })
.implement("c" {a:'', b:''});
})(MyFunction);
You can also do the following instead of using the "Object.create()" method. 您也可以执行以下操作,而不是使用“Object.create()”方法。
Function call: 功能调用:
com.blah.MyChildObj.prototype = createObject(com.blah.MyParentObj.prototype,
com.blah.MyChildObj);
Function definition: 功能定义:
function createObject(theProto, theConst) {
function x() {};
x.prototype = theProto;
x.prototype.constructor = theConst;
return new x();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.