JavaScript构造函数-什么时候发生错误?

时间:2019-07-02 19:10:59

标签: javascript constructor

Нello!在javascript中,几乎所有值都可以成功检索其onda install snowflake-sqlalchemy

(base) C:\Windows\system32>conda info

     active environment : base
    active env location : C:\ProgramData\Anaconda3
            shell level : 1
       user config file : C:\Users\localadmin\.condarc
 populated config files : C:\Users\localadmin\.condarc
          conda version : 4.5.2
    conda-build version : 3.15.1
         python version : 3.5.6.final.0
       base environment : C:\ProgramData\Anaconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/win-64
                          https://repo.anaconda.com/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/pro/win-64
                          https://repo.anaconda.com/pkgs/pro/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
          package cache : C:\ProgramData\Anaconda3\pkgs
                          C:\Users\localadmin\AppData\Local\conda\conda\pkgs
       envs directories : C:\ProgramData\Anaconda3\envs
                          C:\Users\localadmin\AppData\Local\conda\conda\envs
                          C:\Users\localadmin\.conda\envs
               platform : win-64
             user-agent : conda/4.5.2 requests/2.19.1 CPython/3.5.6 Windows/7 Wi
ndows/6.1.7601
          administrator : True
             netrc file : None
           offline mode : False


(base) C:\Windows\system32>conda list
# packages in environment at C:\ProgramData\Anaconda3:
#
# Name                    Version                   Build  Channel
_ipyw_jlab_nb_ext_conf    0.1.0                    py35_0
alabaster                 0.7.11                   py35_0
anaconda-clean            1.1.0                    py35_1
anaconda-client           1.7.2                    py35_0
anaconda-navigator        1.9.2                    py35_0
anaconda-project          0.8.2                    py35_0
asn1crypto                0.24.0                   py35_0
astroid                   2.0.4                    py35_0
astropy                   3.0.4            py35hfa6e2cd_0
atomicwrites              1.3.0                      py_0
attrs                     19.1.0                     py_0
automat                   0.7.0                    py35_0
babel                     2.7.0                      py_0
backcall                  0.1.0                    py35_0
backports                 1.0                        py_2
backports.shutil_get_terminal_size 1.0.0                    py35_2
beautifulsoup4            4.6.3                    py35_0
bitarray                  0.8.3            py35hfa6e2cd_0
bkcharts                  0.2              py35h4704c85_0
blas                      1.0                         mkl
blaze                     0.11.3                   py35_0
bleach                    2.1.4                    py35_0
blosc                     1.14.3               he51fdeb_0
bokeh                     0.13.0                   py35_0
boto                      2.49.0                   py35_0
boto3                     1.3.1                    py35_0
botocore                  1.4.26                   py35_0
bottleneck                1.2.1            py35h452e1ab_1
bzip2                     1.0.6                hfa6e2cd_5
ca-certificates           2019.5.15                     0
certifi                   2018.8.24                py35_1
cffi                      1.6.0                    py35_0
chardet                   3.0.4                    py35_1
click                     6.7              py35h10df73f_0
cloudpickle               1.1.1                      py_0
clyent                    1.2.2                    py35_1
colorama                  0.3.9            py35h32a752f_0
comtypes                  1.1.7                    py35_0
conda                     4.5.2                    py35_0
conda-build               3.15.1                   py35_0
conda-env                 2.6.0                h36134e3_1
conda-verify              3.1.0                    py35_0
console_shortcut          0.1.1                h6bb2dd7_3
constantly                15.1.0           py35h28b3542_0
contextlib2               0.5.5            py35h0a97e54_0
cryptography              1.4                      py35_0
curl                      7.60.0               h7602738_0
cycler                    0.10.0           py35hcc71164_0
cython                    0.28.5           py35h6538335_0
cytoolz                   0.9.0.1          py35hfa6e2cd_1
dask                      0.19.2                   py35_0
dask-core                 0.19.2                   py35_0
datashape                 0.5.4                    py35_1
decorator                 4.4.0                      py_0
defusedxml                0.6.0                      py_0
distributed               1.23.2                   py35_0
docutils                  0.14             py35h8ccb97f_0
entrypoints               0.2.3                    py35_2
et_xmlfile                1.0.1            py35h2c13def_0
fastcache                 1.0.2            py35hfa6e2cd_2
filelock                  3.0.12                     py_0
flask                     1.0.2                    py35_1
flask-cors                3.0.6                    py35_0
freetype                  2.8                  h51f8f2c_1
future                    0.16.0                   py35_2
get_terminal_size         1.0.0                h38e98db_0
gevent                    1.2.2            py35hb3d9581_0
glob2                     0.6                      py35_0
greenlet                  0.4.15           py35hfa6e2cd_0
h5py                      2.8.0            py35h3bdd7fb_2
hdf5                      1.10.2               hac2f561_1
heapdict                  1.0.0                    py35_2
html5lib                  1.0.1                    py35_0
hyperlink                 19.0.0                     py_0
icc_rt                    2017.0.4             h97af966_0
icu                       58.2                 ha66f8fd_1
idna                      2.7                      py35_0
imageio                   2.4.1                    py35_0
imagesize                 1.1.0                    py35_0
incremental               17.5.0                   py35_0
intel-openmp              2018.0.0                      8
ipykernel                 4.10.0                   py35_0
ipython                   6.5.0                    py35_0
ipython_genutils          0.2.0            py35ha709e79_0
ipywidgets                7.4.1                    py35_0
isort                     4.3.4                    py35_0
itsdangerous              0.24                     py35_1
jdcal                     1.4.1                      py_0
jedi                      0.12.1                   py35_0
jinja2                    2.10                     py35_0
jmespath                  0.9.4                      py_0
jpeg                      9b                   hb83a4c4_2
jsonschema                2.6.0            py35h27d56d3_0
jupyter                   1.0.0                    py35_7
jupyter_client            5.2.3                    py35_0
jupyter_console           5.2.0                    py35_1
jupyter_core              4.4.0                    py35_0
jupyterlab                0.34.9                   py35_0
jupyterlab_launcher       0.13.1                   py35_0
keyring                   13.2.1                   py35_0
kiwisolver                1.0.1            py35h6538335_0
lazy-object-proxy         1.3.1            py35hfa6e2cd_2
libcurl                   7.60.0               hc4dcbb0_0
libiconv                  1.15                 h1df5818_7
libpng                    1.6.34               h79bbb47_0
libsodium                 1.0.16               h9d3ae62_0
libssh2                   1.8.0                hd619d38_4
libtiff                   4.0.9                hb8ad9f9_1
libxml2                   2.9.8                hadb2253_1
libxslt                   1.1.32               hf6f1972_0
llvmlite                  0.24.0           py35h6538335_0
locket                    0.2.0            py35h0dfcdd0_1
lxml                      4.2.5            py35hef2cd61_0
lzo                       2.10                 h6df0209_2
m2w64-gcc-libgfortran     5.3.0                         6
m2w64-gcc-libs            5.3.0                         7
m2w64-gcc-libs-core       5.3.0                         7
m2w64-gmp                 6.1.0                         2
m2w64-libwinpthread-git   5.0.0.4634.697f757               2
markupsafe                1.0              py35hfa6e2cd_1
matplotlib                2.2.2            py35h153e9ff_1
mccabe                    0.6.1                    py35_1
menuinst                  1.4.14           py35hfa6e2cd_0
mistune                   0.8.3            py35hfa6e2cd_1
mkl                       2018.0.2                      1
mkl-service               1.1.2            py35hb217b18_4
mkl_fft                   1.0.1            py35h452e1ab_0
mkl_random                1.0.1            py35h9258bd6_0
more-itertools            4.3.0                    py35_0
mpmath                    1.0.0                    py35_2
msgpack-python            0.5.6            py35he980bc4_1
msys2-conda-epoch         20160418                      1
multipledispatch          0.6.0                    py35_0
navigator-updater         0.2.1                    py35_0
nbconvert                 5.5.0                      py_0
nbformat                  4.4.0            py35h908c9d9_0
networkx                  2.3                        py_0
nltk                      3.3.0                    py35_0
nose                      1.3.7                    py35_2
notebook                  5.6.0                    py35_0
numba                     0.39.0           py35h830ac7b_0
numexpr                   2.6.5            py35hcd2f87e_0
numpy                     1.14.3           py35h9fa60d3_1
numpy-base                1.14.3           py35h555522e_1
numpydoc                  0.9.1                      py_0
odo                       0.5.1            py35hc850252_0
olefile                   0.46                     py35_0
openpyxl                  2.6.2                      py_0
openssl                   1.0.2s               he774522_0
packaging                 17.1                     py35_0
pandas                    0.23.4           py35h830ac7b_0
pandoc                    1.19.2.1             hb2460c7_1
pandocfilters             1.4.2                    py35_1
parso                     0.4.0                      py_0
partd                     0.3.10                     py_0
path.py                   11.1.0                   py35_0
pathlib2                  2.3.2                    py35_0
patsy                     0.5.0                    py35_0
pep8                      1.7.1                    py35_0
pickleshare               0.7.4            py35h2f9f535_0
pillow                    5.1.0            py35h0738816_0
pip                       10.0.1                   py35_0
pkginfo                   1.4.2                    py35_1
pluggy                    0.11.0                     py_0
ply                       3.11                     py35_0
prometheus_client         0.3.1            py35h28b3542_0
prompt_toolkit            1.0.15           py35h89c7cb4_0
psutil                    5.4.7            py35hfa6e2cd_0
py                        1.6.0                    py35_0
pyasn1                    0.4.5                      py_0
pyasn1-modules            0.2.2                    py35_0
pycodestyle               2.4.0                    py35_0
pycosat                   0.6.3            py35hfa6e2cd_0
pycparser                 2.19                     py35_0
pycrypto                  2.6.1            py35hfa6e2cd_9
pycurl                    7.43.0.2         py35h74b6da3_0
pyflakes                  2.0.0                    py35_0
pygments                  2.4.2                      py_0
pylint                    2.1.1                    py35_0
pyodbc                    4.0.24           py35h6538335_0
pyopenssl                 0.16.0                   py35_0
pyparsing                 2.4.0                      py_0
pyqt                      5.9.2            py35h1aa27d4_0
pysocks                   1.6.8                    py35_0
pytables                  3.4.4            py35he6f6034_0
pytest                    3.8.1                    py35_0
pytest-arraydiff          0.2              py35h39e3cac_0
pytest-astropy            0.4.0                    py35_0
pytest-doctestplus        0.1.3                    py35_0
pytest-openfiles          0.3.0                    py35_0
pytest-remotedata         0.3.0                    py35_0
python                    3.5.6                he025d50_0
python-dateutil           2.7.3                    py35_0
pytz                      2019.1                     py_0
pywavelets                1.0.0            py35h452e1ab_0
pywin32                   223              py35hfa6e2cd_1
pywinpty                  0.5.4                    py35_0
pyyaml                    3.13             py35hfa6e2cd_0
pyzmq                     17.1.2           py35hfa6e2cd_0
qt                        5.9.5            vc14he4a7d60_0  [vc14]
qtawesome                 0.5.7                      py_0
qtconsole                 4.5.1                      py_0
qtpy                      1.7.1                      py_0
requests                  2.19.1                   py35_0
rope                      0.14.0                     py_0
ruamel_yaml               0.15.46          py35hfa6e2cd_0
scikit-image              0.14.0           py35h6538335_1
scikit-learn              0.19.1           py35h2037775_0
scipy                     1.1.0            py35h672f292_0
seaborn                   0.9.0                    py35_0
send2trash                1.5.0                    py35_0
setuptools                40.2.0                   py35_0
simplegeneric             0.8.1                    py35_2
singledispatch            3.4.0.3          py35h33f66b4_0
sip                       4.19.8           py35h6538335_0
six                       1.11.0                   py35_1
snappy                    1.1.7                h777316e_3
snowballstemmer           1.2.1            py35h4c55bfa_0
snowflake_connector_python 1.2.1.1                  py35_0    snowflakedb
sortedcollections         1.0.1                    py35_0
sortedcontainers          2.0.5                    py35_0
sphinx                    2.1.0                      py_0
sphinxcontrib             1.0                      py35_1
sphinxcontrib-applehelp   1.0.1                      py_0
sphinxcontrib-devhelp     1.0.1                      py_0
sphinxcontrib-htmlhelp    1.0.2                      py_0
sphinxcontrib-jsmath      1.0.1                      py_0
sphinxcontrib-qthelp      1.0.2                      py_0
sphinxcontrib-serializinghtml 1.1.3                      py_0
sphinxcontrib-websupport  1.1.2                      py_0
spyder                    3.3.1                    py35_1
spyder-kernels            0.2.6                    py35_0
sqlalchemy                1.2.11           py35hfa6e2cd_0    anaconda
sqlite                    3.23.1               h35aae40_0
statsmodels               0.9.0            py35h452e1ab_0
sympy                     1.2                      py35_0
tblib                     1.4.0                      py_0
terminado                 0.8.1                    py35_1
testpath                  0.3.1            py35h06cf69e_0
tk                        8.6.7                hcb92d03_3
toolz                     0.9.0                    py35_0
tornado                   5.1.1            py35hfa6e2cd_0
tqdm                      4.32.1                     py_0
traitlets                 4.3.2            py35h09b975b_0
twisted                   17.5.0                   py35_0
typed-ast                 1.1.0            py35hfa6e2cd_0
typing                    3.6.6                    py35_0
unicodecsv                0.14.1           py35h0d88516_0
urllib3                   1.23                     py35_0
vc                        14.1                 h21ff451_3    anaconda
vs2015_runtime            15.5.2                        3    anaconda
wcwidth                   0.1.7            py35h6e80d8a_0
webencodings              0.5.1                    py35_1
werkzeug                  0.15.4                     py_0
wheel                     0.31.1                   py35_0
widgetsnbextension        3.4.1                    py35_0
win_inet_pton             1.0.1                    py35_1
win_unicode_console       0.5              py35h56988b5_0
wincertstore              0.2              py35hfebbdb8_0
winpty                    0.4.3                         4
wrapt                     1.10.11          py35hfa6e2cd_2
xlrd                      1.1.0                    py35_1
xlsxwriter                1.1.8                      py_0
xlwings                   0.11.8                   py35_0
xlwt                      1.3.0            py35hd04410a_0
yaml                      0.1.7                hc54c509_2
zeromq                    4.2.5                hc6251cf_0
zict                      0.1.3                    py35_0
zlib                      1.2.11               h8395fce_2
zope                      1.0                      py35_1
zope.interface            4.5.0            py35hfa6e2cd_0

我也知道您可以创建一个其实例不具有constructor属性的类:

console.log((33).constructor); // Number
console.log((false).constructor); // Boolean
console.log(('hello').constructor); // String
console.log(({}).constructor); // Object
console.log(([]).constructor); // Array
console.log((v => v).constructor); // Function

我感兴趣的是在尝试检索其构造函数时会导致错误的值。我只知道两个这样的值:

constructor

我的问题是: let O = function(){}; O.prototype = Object.create(null); console.log((new O()).constructor)); // undefined console.log((null).constructor); // TypeError! console.log((undefined).constructor); // TypeError! 是仅有两个可能具有这种行为的值吗?

1 个答案:

答案 0 :(得分:4)

  

我的问题是:具有这种行为的唯一两个可能值是null和undefined吗?

默认情况下是,因为如果对象或其任何原型上不存在属性,则将获得值undefined。抛出nullundefined的原因是您无法尝试从它们获取属性。当您尝试从中获取属性时,它们是唯一表现出这种行为的东西。

但是,当您直接获得constructor属性时,可以构造任何对象引发错误:

const o = {
  get constructor() {
    throw new Error("Don't do that!");
  }
};
console.log(o.constructor);

或在陷阱阻止的情况下使用代理:

const o = new Proxy({}, {
  get(target, propName, receiver) {
    if (propName === "constructor") {
      throw new Error("Don't do that!");
    }
    return Reflect.get(target, propName, receiver);
  }
});
console.log(o.constructor);