在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.
}
Run Code Online (Sandbox Code Playgroud)
这到目前为止都是正确的吗?
然后有人喜欢自动执行匿名函数方法来创建命名空间.有什么意义,当你有这样的方式做同样的事情,提供一个命名空间?
最后你有我不明白的对象字面符号.
var object = { // Something strange in here }
Run Code Online (Sandbox Code Playgroud)
那里发生了什么?是JSON吗?它是如何使用的,我该如何使用它.使用这种方式而不是使用我描述的方法有什么好处?你为什么要原型而不是第一次正确地上课?