我正在尝试在这里创建一个新对象。 这是我的代码
function createAnObject(name, address) {
var newObj = new Object();
newObj.name = name;
newObj.address = address;
newObj.saySomething = function () {
console.log("the name is" + this.name + " the addess is" + this.address)
}
}
var ballack = createAnObject('ballack', 'Ndri');
console.log(ballack.name);
但在控制台中错误是
Uncaught TypeError: Cannot read property 'name' of undefined
我在这里很困惑。有人能告诉我哪里出错了。
答案 0 :(得分:1)
您忘记在import os
def ensure_directory_exists(path_directory):
if not os.path.exists(path_directory):
os.makedirs(path_directory)
def os_path_separators():
seps = []
for sep in os.path.sep, os.path.altsep:
if sep:
seps.append(sep)
return seps
def sanitise_filesystem_name(potential_file_path_name):
# Sort out unicode characters
valid_filename = normalize('NFKD', potential_file_path_name).encode('ascii', 'ignore').decode('ascii')
# Replace path separators with underscores
for sep in os_path_separators():
valid_filename = valid_filename.replace(sep, '_')
# Ensure only valid characters
valid_chars = "-_.() {0}{1}".format(string.ascii_letters, string.digits)
valid_filename = "".join(ch for ch in valid_filename if ch in valid_chars)
# Ensure at least one letter or number to ignore names such as '..'
valid_chars = "{0}{1}".format(string.ascii_letters, string.digits)
test_filename = "".join(ch for ch in potential_file_path_name if ch in valid_chars)
if len(test_filename) == 0:
# Replace empty file name or file path part with the following
valid_filename = "(Empty Name)"
return valid_filename
def get_root_path():
# Replace with your own root file path, e.g. '/place/to/save/files/'
filepath = get_file_root_from_config()
filepath = os.path.abspath(filepath)
# ensure trailing path separator (/)
if not any(filepath[-1] == sep for sep in os_path_separators()):
filepath = '{0}{1}'.format(filepath, os.path.sep)
ensure_directory_exists(filepath)
return filepath
def path_split_into_list(path):
# Gets all parts of the path as a list, excluding path separators
parts = []
while True:
newpath, tail = os.path.split(path)
if newpath == path:
assert not tail
if path and path not in os_path_separators():
parts.append(path)
break
if tail and tail not in os_path_separators():
parts.append(tail)
path = newpath
parts.reverse()
return parts
def sanitise_filesystem_path(potential_file_path):
# Splits up a path and sanitises the name of each part separately
path_parts_list = path_split_into_list(potential_file_path)
sanitised_path = ''
for path_component in path_parts_list:
sanitised_path = '{0}{1}{2}'.format(sanitised_path, sanitise_filesystem_name(path_component), os.path.sep)
return sanitised_path
def check_if_path_is_under(parent_path, child_path):
# Using the function to split paths into lists of component parts, check that one path is underneath another
child_parts = path_split_into_list(child_path)
parent_parts = path_split_into_list(parent_path)
if len(parent_parts) > len(child_parts):
return False
return all(part1==part2 for part1, part2 in zip(child_parts, parent_parts))
def make_valid_file_path(path=None, filename=None):
root_path = get_root_path()
if path:
sanitised_path = sanitise_filesystem_path(path)
if filename:
sanitised_filename = sanitise_filesystem_name(filename)
complete_path = os.path.join(root_path, sanitised_path, sanitised_filename)
else:
complete_path = os.path.join(root_path, sanitised_path)
else:
if filename:
sanitised_filename = sanitise_filesystem_name(filename)
complete_path = os.path.join(root_path, sanitised_filename)
else:
complete_path = complete_path
complete_path = os.path.abspath(complete_path)
if check_if_path_is_under(root_path, complete_path):
return complete_path
else:
return None
函数中退回newObj
:
createAnObject
答案 1 :(得分:0)
在现代JS中:
function createAnObject(name, address) {
return {
name,
address,
saySomething() {
console.log("the name is", this.name, "the address is", this.address);
}
};
}
var ballack = createAnObject('ballack', 'Ndri');
console.log(ballack.name);
答案 2 :(得分:-1)
您没有返回创建的对象。
var date1 = new Date(), date2 = new Date(); // init vars
date1.compareTo(date2); // or
date2.compareTo(date1);
返回newObj; };
function createAnObject(name, address) {
var newObj = new Object();
newObj.name = name;
newObj.address = address;
newObj.saySomething = function () {
console.log("the name is" + this.name + " the addess is" + this.address)
}
上面的工作方法和这里有一个jsbin工作:http://jsbin.com/xagisanema/edit?html,js,console
答案 3 :(得分:-1)
您必须在newObj
方法中返回createAnObject
。
只需在方法末尾添加返回值
return newObj;