gpt4 book ai didi

javascript - 如何在 JavaScript 中创建一个类?

转载 作者:行者123 更新时间:2023-11-29 16:18:16 25 4
gpt4 key购买 nike

在 JavaScript 中有很多方法可以做同样的事情。然而,我已经掌握了一些方法,还有一些我坦率地说不明白的方法。谁能帮我澄清一些事情? (我首先在 PHP 中学习了 OOP。)

所以类可以这样写:

var object = new class(constructparams) {
var private_members; // Can be accessed from within the code inside this definition only.
this.public_members; // Can be accessed from everywhere.

var private_functions = function() {}
this.public_functions = function() {}
}

object.prototype.semi_public_members = function() {
// Will be public, but can only access public members and methods.
// E. g. private_members; is not available here.
}

到目前为止这一切都正确吗?

然后有人喜欢自执行匿名函数的方式来创建命名空间。当您在上面使用这种方式做同样的事情时,提供命名空间的意义何在?

最后,你有我不理解的对象字面量符号。

var object = { // Something strange in here }

里面发生了什么?是JSON吗?怎么用,我怎么用。使用这种方式而不是使用我描述的方法有什么好处?为什么要制作原型(prototype)而不是第一次就正确地制作类?

最佳答案

通过示例解释构造对象中不同事物的行为:

// Defined as a variable from an anonymous function
// so that there is scope closure over variables
// shared across all instances and the prototype.
// If this isn't important, you don't need to close
// scope around it, so define directly
var ConstructedObject = (function constructorCreator () {
// Define any variables/methods to be shared across
// all instances but not polluting the namespace
var sharedVariable = 'foo';

// Next the actual constructor
function ConstructedObject () {
// Variables here are normally used to help
// each instance and will be kept in memory as
// long as the instance exists
var instanceVariable = 'bar';
// instance-specific properties get defined
// using the "this" keyword, these are the
// properties expected to be changed across
// each different instance
this.instanceProperty = true;
this.instanceMethod = function () { return instanceVariable; };
this.changeInstanceVar = function () { instanceVariable = 'foo'; };
// you do have access to the shared
// variables here if you need them.
}
// After the constructor, you set up the
// prototype, if any. This is an object of shared
// properties and methods to be inherited by every
// instance made by the constructor, and it also
// inherits the prototype's prototype, too.
// Lets use a literal object for simplicity.
ConstructedObject.prototype = {
// Accessing the instance to which a method
// applies is done using the "this" keyword,
// similar to in the constructor
sharedMethod : function () { return [sharedVariable, this.instanceMethod(),this.instanceProperty]; },
changeSharedVar : function () { sharedVariable = 'bar'; }
// properties may also be defined
};
// Finally, the constructor is returned so it
// can be kept alive outside of the anonymous
// function used to create it
return ConstructedObject;
// and the anonymous function is called to execute
// what we've done so far
})();

执行完上面的代码后,您就有了一个构造函数,它可以创建具有实例特定变量和共享变量的对象。现在让我们通过创建两个实例并比较它们在某些更改前后的行为来了解它们的行为。

// First create the two instances
var myObjA = new ConstructedObject(),
myObjB = new ConstructedObject();
// Now compare them, the sharedMethod method we
// used in the prototype offers an easy way to
// do this
console.log( myObjA.sharedMethod(), myObjB.sharedMethod() );
// ["foo", "bar", true] ["foo", "bar", true]
// Next lets change the different variables in
// myObjB so we can see what happens, again the
// change* methods defined before let us do this
// easily
myObjB.changeInstanceVar();
myObjB.changeSharedVar();
// For completeness, lets also change the property
// on myObjB.
myObjB.instanceProperty = false;
// Now when we compare them again, we see that our
// changes to the myObjB instance have only changed
// the shared variables of myObjA
console.log( myObjA.sharedMethod(), myObjB.sharedMethod() );
// ["bar", "bar", true] ["bar", "foo", false]

为了方便查看,这里将两条记录语句放在一起

//     myObjA               myObjB
["foo", "bar", true] ["foo", "bar", true]
["bar", "bar", true] ["bar", "foo", false]

关于javascript - 如何在 JavaScript 中创建一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13213928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com