I have Suitescript 1.0 and Suitescript 2.0 client ...
# suitescript
d
I have Suitescript 1.0 and Suitescript 2.0 client scripts in Purchase order The variable self in SS1 showing different value with SS2 activated ---- > self = windows object with SS2 nonactivated --->> self = object of MY_PO Anybody knows why the self variable showing different value ? Thanks This is my SS1
Copy code
if (typeof MY_PO === 'undefined') var MY_PO = {};

MY_PO.headData = 'head';
MY_PO.pageInit = function (type) {
	var self = this;
}
This is my SS2
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/record'], function (record) {
        function pageInit(scriptContext) {
            log.debug('test','test');
        }

        return {
            pageInit: pageInit,
        };
    });
b
i wouldnt expect the code you shared to have different values of this
but if you tried to translate the ss1 code to ss2 code, there could be problems
d
i debug using chrome dev tools, indeed the var self value is different
with and without the SS2
also with SS2 active, self.headData = undefined self.MY_PO.headData is working
but without SS2, self.headData is working fine
b
the code you shared does nothing with
self
(which is really
this
)
so there cant really be any problems with it
the value of
this
depends heavily on how its used
the code you shared doesnt use it, so there cant be any problems with it
d
i removed some of the lines for brevity
in my complete code i need to set self.headData.id = 'xxx' , and i get error self.headData = undefined
the SS1 code is working fine in production, but now there is a need to add a new client script, and i use SS2 for the new script, then the SS1 starts to break
i traced the problem is on the self variable, as above mentioned with ss2 , self = windows object without ss2 , self = the MY_PO object
b
still not sure what you are trying to do
but ill give the example of why the context matters
for your ss1 code
if it was
Copy code
if (typeof MY_PO === 'undefined') var MY_PO = {};

MY_PO.headData = 'head';
MY_PO.pageInit = function (type) {
	console.log(this)
}
Copy code
MY_PO.pageInit()
would log
MY_PO
Copy code
var fn = MY_PO.pageInit
fn()
would log
window
d
Copy code
if (typeof MY_PO === 'undefined') var MY_PO = {};

MY_PO.headData = 'head';
MY_PO.pageInit = function (type) {
	console.log(this)
}
so you're saying this will log MY_PO object ?
b
no, im saying that the code does nothing
Copy code
if (typeof MY_PO === 'undefined') var MY_PO = {};

MY_PO.headData = 'head';
MY_PO.pageInit = function (type) {
	console.log(this)
}

MY_PO.pageInit()
will log MY_PO
d
yes, i do call the pageInit function in the Script Record, under PAGE INIT FUNCTION, if that's what you mean
b
you dont actually control that
d
wait let me screen shot
b
you dont know if its doing
Copy code
MY_PO.pageInit()
or
Copy code
var fn = MY_PO.pageInit
fn()
to call your code
it could be doing both, it could be doing neither
d
message has been deleted
message has been deleted
i expect the console.log should show MY_PO object or am i wrong ?
b
the value of
this
is determined by the calling code
you shouldn't have any expectations of what value is in
this
you dont control the calling code
d
ic, so you're saying if NS calls another script before my script, the value of
this
could be different ?
b
it could be different for any arbitrary reason
d
hmm ic
hmm ok, i think i got what you mean, just want to show you this, if i disabled my SS2 client script which is also running on Purchase order, i got this
so i think i should replace the use of
this
keyword
thanks @battk as always, you're always helpful