javascript - Object.create() says parameter is not an object -
description
in program reading json file, parsing object , try "cast" object of class projectfile
using object.create()
.
code
let tmpfilecontent = fs.readfilesync(tmppath, {encoding: 'utf-8'}); let tmpobject = json.parse(tmpfilecontent); console.log(tmpobject); filelist[filelist.length] = object.create(projectfile, tmpobject);
log
question
when output tmpobject
using console.log(tmpobject);
says object in log. in line after try use object should casted object of class projectfile displays error message not object. doing wrong?
edit: projectfile class
class projectfile { constructor(p_name, p_path, p_type, p_thumnailpath) { this.name = p_name; this.path = p_path; this.thumnailpath = p_thumnailpath; } }
edit 2: working code
let tmpfilecontent = fs.readfilesync(tmppath, {encoding: 'utf-8'}); let tmpobject = json.parse(tmpfilecontent); console.log(tmpobject); filelist[filelist.length] = object.create(projectfile, { name: { value: tmpobject.name, writable: true, enumerable: true, configurable: true }, path: { value: tmpobject.path, writable: true, enumerable: true, configurable: true }, thumnailpath: { value: tmpobject.thumnailpath, writable: true, enumerable: true, configurable: true } });
object.create function gets prototype first parameter , property descriptors second parameter.
your second parameter has wrong type. need pass object, contains objects property attributes configurable
, writable
, enumerable
, value
it.
see example. in second case when pass parameter not apply desired shape, gives me same error.
const pr = { name: 'name' }; const successchild = object.create( pr, { surname: { value: 'surname', writable: true, enumerable: true, configurable: true } }); console.log(successchild); const errorchild = object.create( pr, { name: 'error name', surname: 'error surname' });
Comments
Post a Comment