Javascript 有没有更惯用的写作方法?
我有点担心这个问题不是这个问题的正确形式,所以如果有更好的方式表达/提出这个问题,请让我知道 基本上,前几天我在读一本关于某个人制作的坐标系的书。这很酷,我在想如何让自己变得更好。所以我做了 简言之,它将墨卡托地图递归地划分为36个区域,每个区域由数字或字母大写表示 无论如何,我用一个称为区域的Javascript对象类来表示这一点。但是,构造函数使用一些对象的原型方法。不知道还能做什么,我做了这件事你可以忽略它的实质,我主要是询问对象结构:Javascript 有没有更惯用的写作方法?,javascript,constructor,prototype,Javascript,Constructor,Prototype,我有点担心这个问题不是这个问题的正确形式,所以如果有更好的方式表达/提出这个问题,请让我知道 基本上,前几天我在读一本关于某个人制作的坐标系的书。这很酷,我在想如何让自己变得更好。所以我做了 简言之,它将墨卡托地图递归地划分为36个区域,每个区域由数字或字母大写表示 无论如何,我用一个称为区域的Javascript对象类来表示这一点。但是,构造函数使用一些对象的原型方法。不知道还能做什么,我做了这件事你可以忽略它的实质,我主要是询问对象结构: // Internally, zones are r
// Internally, zones are represented from (0,0) to (1,1), as in the positive quadrant
// on the Cartesian plane. Since it is just a square, we only need the bottomLeft
// point and a 'zoomLevel' to determine the side lengths, which is (1/6)**zoomLevel.
var Zone = function () { this.constructor.apply(this,arguments) }
Zone.prototype = {
constructor: function(address) {
if (!address) {
this.x = 0
this.y = 0
this.address = ""
this.zoomLevel = 0
} else {
this.address = address.toString(36)
coords = hexcoords(this.address.slice(0,1))
this.x = coords[0]
this.y = coords[1]
this.zoomLevel = 1
}
while (this.zoomLevel < this.address.length) {
this.descend(this.address.slice(this.zoomLevel, this.zoomLevel+1), true)
}
},
topRight: function () { return [ this.x + Math.pow((1/6),this.zoomLevel),
this.y + Math.pow((1/6),this.zoomLevel) ] },
toLatLong: function (x,y) {
// top left corner is -180 longitude and 85.05112878
if (!x && !y) {
// latitude and longitude are kind of reversed from x/y
return [85.05112878 - 170.1022*(1-this.y), -180 + 360 * this.x ]
}
return [85.05112878 - 170.1022*(1-y), -180 + 360 * x ]
},
clone: function () { return new Zone(this.address) },
// Descending is basically just always moving the bottomLeft point some nonnegative
// amount in the positive direction. Luckily, the subzone allows us to calculate
// that easily.
descend: function (zone, dontchangeaddress) {
this.x += Math.pow((1/6),this.zoomLevel)*hexcoords(zone)[0]
this.y += Math.pow((1/6),this.zoomLevel)*hexcoords(zone)[1]
if (!dontchangeaddress) {
this.address = this.address.concat(zone.toString(36))
}
this.zoomLevel += 1
return this
}
}
基本上,我的问题是,是否有更好或更惯用的方法来定义对象及其属性,以便在构造函数中使用它们
如果需要的话,您可以随意提出其他形式的批评或建议。您可以放心地写作
Zone = function(address) {
if (!address) {
this.x = 0
this.y = 0
this.address = ""
this.zoomLevel = 0
} else {
this.address = address.toString(36)
coords = hexcoords(this.address.slice(0,1))
this.x = coords[0]
this.y = coords[1]
this.zoomLevel = 1
}
while (this.zoomLevel < this.address.length) {
this.descend(this.address.slice(this.zoomLevel, this.zoomLevel+1), true)
}
};
或者更广义地说:
在Javascript中,任何函数都可以引用在定义函数时似乎不存在的对象属性。这是因为在函数实际被调用之前,即在运行时,引用不会被解析。Herm。你看,我原以为是这样的,当我把它转换成使用原型方法时,我就是这样写的,但后来我就崩溃了。我想我还有一些不知道的错误/不知道你为什么这么做。如何使this.constructor的使用比this.down更安全?根据我对您答案的回答,我最初认为它还没有被评估,但它向我抛出了模糊的错误,我无法快速调试。我猜想,以这种方式定义原型要么在构建之前不会得到评估,要么以“一次”定义所有内容的方式进行评估。这不是很科学,也不是经过深思熟虑的。我最初的直觉是正确的,我只是在早期的某个地方犯了一个错误。
Zone.prototype = ...
new Zone(); // ok