gpt4 book ai didi

javascript - 最后定义的原型(prototype)函数总是在对象初始化时运行

转载 作者:行者123 更新时间:2023-12-02 18:09:08 25 4
gpt4 key购买 nike

给出以下代码(StoriesMap.js)...

// Initalization; Creates Map
var StoriesMap = function StoriesMap( id, location, tileset ) {
this.map = L.map( id ).setView([ location.lat, location.lon ], location.zoom);

// Add inital points to map
this.populate( location );

// Redraw on map movement
this.map.on("viewreset", function( event ) {
var location = {
lat: event.target._animateToCenter.lat,
lon: event.target._animateToCenter.lng,
zoom: event.target._animateToZoom
};

this.redraw( location );
});

var mapTiles = L.tileLayer( tileset.url, { attribution: tileset.attribution, minZoom: 4, maxZoom: 12 } ).addTo( this.map );
}


// Put some points on the map
StoriesMap.prototype.populate = function populate( location ) {
var priority = location.zoom - 3;

if ( typeof points === "object" ) {
var buffer = [];

for ( var i in points ) {
var point = points[i];

if ( point.priority <= priority ) {
var circle = L.circle([ point.lat, point.lon ], 100000, {
color: '#f03', fillColor: '#f03', fillOpacity: 0.5
}).addTo( this.map );
}
}
}

}

// Filters map contents
StoriesMap.prototype.filter = function filter( map, location ) {
console.log( "filter" );
}

// Redraws map points
StoriesMap.prototype.redraw = function redraw( map, location ) {
console.log ( this.map )

for ( var i in map._layers ) {
if ( map._layers[i]._path != undefined ) {
try {
map.removeLayer( map._layers[i] );
}
catch(e) {
console.log("problem with " + e + map._layers[i]);
}
}
}
}

StoriesMap.prototype.fake = function fake() {
console.log("Always Called when object is Initialized");
}

当我运行时(从我的html):

 var map = new Map();

我总是得到:

Always Called when object is Initialized

在我的控制台中。无论出于何种原因,我最后定义的原型(prototype)被视为构造函数,我不确定为什么。我不希望在初始化对象时运行该函数,它是否被假定为构造函数?这种情况以前发生在我身上,我的正常 react 是创建一个名为“fake”的最后一个原型(prototype)函数并将其留空,但是这个问题的正确解决方案是什么?

最佳答案

此代码在某个时刻(可能在某种构建过程或缩小过程中)连接有其他代码。其他代码以括号语句开头:

StoriesMap.prototype.fake = function fake() {
console.log("Always Called when object is Initialized");
}

// start of other code...
(...);

但是,JavaScript 将其读作:

StoriesMap.prototype.fake = function fake() {
console.log("Always Called when object is Initialized");
}(...);

因此,StoriesMap.prototype.fake 不等于该函数,而是使用括号表达式作为参数,立即调用该函数。 StoriesMap.prototype.fake 设置为立即调用函数的返回值

只要在函数表达式末尾添加一个分号,就不会发生这种情况,因为括号内的语句和函数将用分号分隔:

StoriesMap.prototype.fake = function fake() {
console.log("Always Called when object is Initialized");
};
(...);

JavaScript 的自动分号插入仅在缺少分号会导致违反语言语法时发生。见底部ES 7.9.2与此类似的示例:

The source

a = b + c
(d + e).print()

is not transformed by automatic semicolon insertion, because the parenthesised expression that begins the second line can be interpreted as an argument list for a function call:

a = b + c(d + e).print()

In the circumstance that an assignment statement must begin with a left parenthesis, it is a good idea for the programmer to provide an explicit semicolon at the end of the preceding statement rather than to rely on automatic semicolon insertion.

关于javascript - 最后定义的原型(prototype)函数总是在对象初始化时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19844376/

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