我正在寻找关于这段代码的澄清:
// Taken from alert create class call for clarification
_createClass(Alert, null, [{
key: 'VERSION',
get: function get() {
return VERSION;
}
}]);
return Alert;
// Taken from BS boilerplate
var _createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) {
descriptor.writable = true;
}
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) {
defineProperties(Constructor.prototype, protoProps);
}
if (staticProps) {
defineProperties(Constructor, staticProps);
}
return Constructor;
};
}();
我不确定参数Alert
,null
和数组对象在不接受任何参数时如何传递给createClass
。我假设他们以某种方式得到了其他嵌套函数,但我不确定如何。
答案 0 :(得分:0)
{
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
},
{
"type": "mobile",
"number": "123 456-7890"
}
]
}
被分配了该函数的结果。这是因为该函数是一个立即调用的函数表达式(IIFE) - 因此它的末尾是_createClass
。
所以()
真的是:
_createClass
答案 1 :(得分:0)
我立即删除了调用的函数表达式(IIFE)包装器
defineProperties
为了清晰起见,并将defineProperties
功能移到了外面。
但我需要说IIFE包装器首先是在IIFE之外没有泄漏const defineProperties = (/*Alert*/target, /*[{...}]*/props) => {
props.forEach(function(/*{...}*/descriptor) {
// assigning false because we don't have it specified
// in props object
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
// we don't have value either, so - skipping...
if ("value" in descriptor) descriptor.writable = true;
// assigning VERSION prop with earlier decorated descriptor {...}
Object.defineProperty(target, descriptor.key, descriptor);
// Now our Alert function has VERSION key, which returns 'VERSION' with its getter function
});
}
const _createClass = (/*Alert*/Constructor, /*null*/protoProps, /*[{...}]*/staticProps) => {
if (protoProps) {
//not going here obviously because protoProps null
defineProperties(Constructor.prototype, protoProps);
}
if (staticProps) {
// going here because [{...}]
defineProperties(Constructor, staticProps);
}
// returning class with decorated static property VERSION
return Constructor;
};
_createClass(
Alert, //Constructor
null, //protoProps
[{ //staticProps
key: 'VERSION',
get: function get() {
return VERSION;
}
}]
);
功能。因此,在大多数情况下,基本上需要IIFE来包含一次创建的所有私有变量。
除此之外,请看一下这个调试风格的逐步重构版本。
import tkinter.font
from tkinter import *
root = Tk()
# Get a font object
font_obj = tkinter.font.Font()
canvas = Canvas(root)
canvas.grid()
string_list = []
string_list.append('the text that is too wide for the canvas and I need to know that it is')
string_list.append('this text will fit')
# Iterate through the strings, getting the number of pixels it takes.
# Note that this can be configured larger or smaller based on need.
can_width = int(canvas['width'])
i = 0
for s in string_list:
str_pixels = font_obj.measure(s, canvas)
# Compare the width of the canvas to the number of str_pixels
if str_pixels <= can_width:
y = (i + 1) * 20
canvas.create_text(10,y,anchor='nw',text=s,font='TkDefault')
i += 1
else:
# Print a warning, ignore, whatever when the string is too long.
print("'" + s + "'", "is too long to display in canvas")
root.mainloop()